欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Jquery1.6版本后attr的变化

发布时间:2023/12/9 编程问答 43 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Jquery1.6版本后attr的变化 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

原文链接:http://www.cnblogs.com/-run/archive/2011/11/16/2251569.html

Jquery1.6版本后attr的变化

Jquery1.6版本后 attr 改动后的效果:
jquery1.6+版本:

 

下文来自www.jquery.com The difference betweenattributes and properties can be important in specific situations.Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior.As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while.attr() retrieves attributes For example, selectedIndex,tagName, nodeName, nodeType, ownerDocument,defaultChecked, and defaultSelected should be retrieved and set with the.prop() method. Prior to jQuery 1.6, these properties were retrievable with the.attr() method, but this was not within the scope of attr. These do not have corresponding attributes and are only properties.

elem.checked$(elem).prop("checked")elem.getAttribute("checked")$(elem).attr("checked")(1.6)$(elem).attr("checked")(1.6.1+)$(elem).attr("checked")(pre-1.6)
true (Boolean) Will change with checkbox state
true (Boolean) Will change with checkbox state
"checked" (String) Initial state of the checkbox; does not change
"checked" (String) Initial state of the checkbox; does not change
"checked" (String) Will change with checkbox state
true (Boolean) Changed with checkbox state

 

 

 

//勾选后输出: //attr('checked'): checked //.prop('checked'): true //.is(':checked'): true//取消勾选输出://.attr('checked'): undefined //.prop('checked'): false //.is(':checked'): false




jquery1.4 版本:

1 <!DOCTYPE html> 2 <html> 3 <head> 4 <style> 5 p { margin: 20px 0 0 } 6 b { color: blue; } 7 </style> 8 <script src="../js/jquery-1.4.4.js"></script> 9 </head> 10 <body> 11 12 <input id="check1" type="checkbox" checked="checked"> 13 <label for="check1">Check me</label> 14 <p></p> 15 16 <script> 17 $("input").change(function() { 18 var $input = $(this); 19 $("p").html(".attr('checked'): <b>" + $input.attr('checked') + "</b><br>" 20 + ".is(':checked'): <b>" + $input.is(':checked') ) + "</b>"; 21 }).change(); 22 </script> 23 24 </body> 25 </html>

 

勾选后输出: //attr('checked'): true //.prop('checked') 1.6后版本才有这个方法 //.is(':checked'): true 取消勾选输出://.attr('checked'): false //.prop('checked')1.6后版本才有这个方法 //.is(':checked'): false


 



结论: attr('checked'): 在1.6后版本,所获取的值是 "checked"/"underfined"  ,之前所获得的值是"false"/"true"。截然不同

 

长知识了

作者:那瞬间
出处:http://www.cnblogs.com/-run/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

总结

以上是生活随笔为你收集整理的Jquery1.6版本后attr的变化的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。