继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

jQuery的prop和attr方法比较

紫衣仙女
关注TA
已关注
手记 366
粉丝 71
获赞 334

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().


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP