jQuery之 prop() VS. attr()

阅读更多


attr() 用于自定义属性,id ;
prop() 用于原生属性,src,href 等。


---------------------------------------------------------------------------

但是,
如果移除属性,

removeProp() 会使值变为 undefined。
removeAttr() 会完全移除。


---------------------------------------------------------------------------



There are two things to note, here:

the first is that the id is an attribute, not a property.
so attr()/removeAttr() should be used instead.

The second is the the API for removeProp() explicitly note that you should not:

...use this method to remove native properties such as checked, disabled, or selected. This will remove the property completely and, once removed, cannot be added again to element. Use .prop() to set these properties to false instead.
Also, rather than unsetting, and then setting, why not simply change it with attr():


$(elem).attr('id', 'newValue');



http://stackoverflow.com/a/12519842/2893073









你可能感兴趣的:(jquery,attr,prop,区别)