jQuery的prop和attr方法之间区别
JQuery.attr():
Get the value of an attribute for the first element in the set of matched elements.
JQuery. Prop():
Gets the value of a property for the first element in the set of matched elements.
Reference MSDN:
for a checkbox (jquery 1.6+)
<input id="check1" checked="checked" type="checkbox" />
.attr(‘checked‘) //returns checked
.prop(‘checked‘) //returns true
.is(‘:checked‘) //returns true
Prop() method returns Boolean value for checked, selected, disabled, readOnly..and so on while attr returns defined string. So, you can directly use .prop("checked") in if condition. SelectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, and defaultSelected..and so on should be retrieved and set with the .prop() method. These do not have corresponding attributes and are only properties. .attr() calls .prop() internally so .attr() method will be slightly slower than accessing them directly through .prop().
一些内容(摘录来自网络)。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。