jQuery's prop() VS. attr()



attr() for custom attributes, id;
prop() for native attributes, src, href, etc.


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

However,
if the property is removed,

removeProp() will make the value undefined.
removeAttr() will remove completely.


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



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









Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326834737&siteId=291194637