使用jQuery时,有些API容易混淆,通过API、源码记录心得如下:
1. $elem.attr() 、 $elem.prop()
1) Attribute values are strings with the exception of a few attributes such as value and tabindex.
Attribute值通常是string类型,除了一些其他的属性,如:value, tabindex.
2) DOM Element Properties generally affect the dynamic state of a DOM element without changing the serialized HTML attribute. .prop() should be used when setting selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, or defaultSelected, checked, disabled, selected.
DOM元素的property通常会改变节点的动态状态,如:checked, disabled, selected.
总结:通常使用.attr(),如果是.prop()则针对上面的属性;