码迷,mamicode.com
首页 > Web开发 > 详细

jQuery的prop和attr方法之间区别

时间:2015-03-06 09:35:37      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

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

 
一些内容(摘录来自网络)。

jQuery的prop和attr方法之间区别

标签:

原文地址:http://www.cnblogs.com/insus/p/4317185.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!