在 jQuery 1.6 以后的版本中,大多数属性可以使用.attr()
获取,但是一些特殊情况,必须使用.prop()
。
参考链接:http://api.jquery.com/prop/ & .prop vs .attr()
第二个链接中有两点说出了attribute(特性)与 property(属性)的区别:
- attribute 的值只能是字符串,而 property 的值可以是任何类型,比如
checked
的 property 是布尔值,style
的 property 是包含单个样式的对象,size
的 property 是数字。 - 虽然一个元素的 attribute 与 property 有相同的名称,更新其中一个通过会更新另一个,但对于一
input
元素一些写入标签的特殊attribute,比如value
与checked
,property 总是表示他的最新状态,而 attribute 表示的是写在代码文件中的初始值( attribute 中的value
与checked
对应 property 中的defaultValue
与defaultChecked
)