码迷,mamicode.com
首页 > 其他好文 > 详细

prop()和attr()

时间:2017-10-06 23:09:33      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:body   attr   tab   重要   复选框   情况下   赋值   标记   class   

attributes和properties(属性值)之间的差异在特定情况下是很重要。jQuery 1.6之前 ,.attr()方法在取某些 attribute 的值时,会返回 property 的值,这就导致了结果的不一致。从 jQuery 1.6 开始, .prop()方法 方法返回 property 的值,而 .attr() 方法返回 attributes 的值。

例如, selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, 和 defaultSelected应使用.prop()方法进行取值或赋值。 在jQuery1.6之前,这些属性使用.attr()方法取得,但是这并不是元素的attr属性。他们没有相应的属性(attributes),只有特性(property)。

 

例如,考虑一个DOM元素的HTML标记中定义的<input type="checkbox" checked="checked" /> ,并假设它是一个JavaScript变量命名的elem :

elem.checked true (Boolean) 将随着复选框状态的改变而改变
$(elem).prop("checked") true (Boolean) 将随着复选框状态的改变而改变
elem.getAttribute("checked") "checked" (String) 复选框的初始状态;不会改变
$(elem).attr("checked") (1.6) "checked" (String) 复选框的初始状态;不会改变
$(elem).attr("checked") (1.6.1+) "checked" (String) 将随着复选框状态的改变而改变
$(elem).attr("checked") (pre-1.6) true (Boolean) 将随着复选框状态的改变而改变

prop()和attr()

标签:body   attr   tab   重要   复选框   情况下   赋值   标记   class   

原文地址:http://www.cnblogs.com/adanxiaobo/p/7633129.html

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