我需要将我的属性动态添加到我的输入中,但它不起作用。
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/