jQuery之 prop() VS. attr()

[size=medium]

attr() 用於自定義屬性,id ;
prop() 用於原生屬性,src,href 等。


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

但是,
如果移除屬性,

removeProp() 會使值變爲 undefined。
removeAttr() 會完全移除。


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


There are two things to note, here:

the first is that the [b]id[/b] 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


[/size]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章