1.attr() 一直存在,prop() 仅存在于 jq-1.6 及其之后
2.新版本jq使用细节:
2.1 自定义添加至dom节点的属性,用attr获取
2.2 表单类checked、selected或disabled操作使用prop
2.3 通过js操作属性使用prop
3.原理:
3.1表单元素的checked、selected、disabled等属性,在 jq-1.6 之前,attr()返回值为被选中(或禁用)就返回true,否则返回false。
但是从1.6开始,使用attr()获取这些属性的返回值为String类型,即如果被选中(或禁用)就返回checked、selected或disabled;否则(即元素节点没有该属性)返回undefined。
并且,在某些版本中,这些属性值表示文档加载时的初始状态值,即使之后更改了这些元素的选中(或禁用)状态,对应的属性值也不会发生改变。
3.2 prop()
函数的设计目标是用于设置或获取指定DOM元素(指的是JS对象,Element类型)上的属性(property);
attr()
函数的设计目标是用于设置或获取指定DOM元素所对应的文档节点上的属性(attribute)。