Adding attribute in jQuery
如何将属性添加到jquery中的特定HTML标记中?
例如,像这个简单的HTML:
1  | <input id="someid" />  | 
然后添加一个disabled="true"属性,如下所示:
1  | <input id="someid" disabled="true" />  | 
你可以使用附加属性:
1  | $('#someid').attr('name', 'value');  | 
然而,适当的性能和
1  | $('#someid').prop('disabled', true);  | 
最佳方案:你可以使用jQuery的支柱从1.6到DDA(物业)
1  | $('#someid').prop('disabled', true);  | 
消除它,使用
1  | $('#someid').removeProp('disabled');  | 
Also note that the .removeProp()
method should not be used to set these
properties to false. Once a native
property is removed, it cannot be
added again. See .removeProp() for
more information.
你可以用这样的
1 2 3 4 5 6 7  | //.attr() $("element").attr("id","newId"); $("element").attr("disabled", true); //.removeAttr() $("element").removeAttr("id"); $("element").removeAttr("disabled");  | 
1  | $('#someid').attr('disabled', 'true');  | 
1  | $('#someid').attr('disabled', 'true');  | 
1  | $('.some_selector').attr('disabled', true);  | 
附加属性:
1  | $('#Selector_id').attr('disabled',true);  | 
使用此代码