javascript - Jquery : how to add attributs dynamically with the function . 属性()

标签 javascript jquery dynamic attributes

我需要将我的属性动态添加到我的输入中,但它不起作用。

  • this.nomRoot 是此处“输入”的组件名称。

  • this.lstAttr 是一个数组,其中包含“name”或“checked”等属性。

  • this.lstAttrValue 是一个数组,其属性值为“myCheckbox”或“true”。

这是我的代码:

Component("Input", {
    add: function(element, type) {
       html = $('<'+this.nomRoot+' type="'+ type +'" />').appendTo('#'+element);

       for(key in this.lstAttr) {
          alert(this.lstAttr[key]);
          alert(this.lstAttrValue[key]);
          $(html).attr("'"+this.lstAttr[key]+"', '"+this.lstAttrValue[key]+"'");
       }

    }
});

所以我的代码使用 firebug 运行但没有添加属性。

我试过类似的东西:

  • this.lstOptions 是一个包含属性和属性值的数组,例如“name, myCheckbox”

    $("<"+this.nomRoot+" />").attr(this.lstOptions).appendTo(element);
    

最佳答案

要设置属性和值,只需要引用这两个索引即可。无需构建某种字符串。

$(html).attr(this.lstAttr[key], this.lstAttrValue[key]);

关于javascript - Jquery : how to add attributs dynamically with the function . 属性(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13614532/

相关文章:

javascript - 如何使用 javascript 将值 bootstrap 下拉列表设置为隐藏值 asp.net

javascript - CSS:如果表头悬停,如何在表的每个单元格上触发悬停?

javascript - 如何制作类似 Pottermore 的动画光标

JavaScript 正则表达式 : Not starting with

javascript - 使用唯一的 HREF 提交表单

javascript - 从kendo multiselect中获取选定的数据数组

jquery - 如何实现一组 Div 元素之间的碰撞检测?

jquery - 动态加载的文本不在 div 内

c++ - 与 C++ 中的指针相关的静态类型类

c++ - 动态数组和运算符重载导致内存崩溃