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
推荐阅读
-
JQuery操作attr、prop、val()/text()/html()、class属性
-
jquery prop的使用介绍及与attr的区别_jquery
-
jquery中attr和prop的区别实例用法详解
-
关于jQuery中.attr()和.prop()的问题探讨
-
jquery prop的使用介绍及与attr的区别
-
Jquery中attr与prop的区别详解
-
jquery中attr和prop的区别解析
-
jQuery中attr()和prop()在修改checked属性时的区别
-
详谈jQuery中使用attr(), prop(), val()获取value的异同
-
jQuery学习之prop和attr的区别示例介绍