欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jQuery之 prop() VS. attr()

程序员文章站 2022-06-10 09:21:40
...


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://*.com/a/12519842/2893073