Jquery1.6版本后attr的变化,灰常委屈
来源:互联网 发布:颓废 知乎 编辑:程序博客网 时间:2024/04/28 20:39
基于上次发表的 Jquery 获取checkbox的checked问题,从大家的评论中得到了答案,原因是Jquery1.6版本后 attr 给改了,悲剧的我不知道,给耍了半天。
认真地去看了官方的最新文档,当中有这样的一段话 = =!
可见,给YIN了吧。下面发个改动后的效果:
jquery1.6+版本:
下文来自www.jquery.com The difference between attributes 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
true
(Boolean) Will change with checkbox state$(elem).prop("checked")
true
(Boolean) Will change with checkbox stateelem.getAttribute("checked")
"checked"
(String) Initial state of the checkbox; does not change$(elem).attr("checked")
(1.6)"checked"
(String) Initial state of the checkbox; does not change$(elem).attr("checked")
(1.6.1+)"checked"
(String) Will change with checkbox state$(elem).attr("checked")
(pre-1.6)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/archive/2011/11/16/2251569.html
- Jquery1.6版本后attr的变化,灰常委屈
- Jquery1.6版本后attr的变化,灰常委屈
- Jquery1.6版本后attr的变化
- jQuery1.6以上版本prop和attr的区别
- jquery1.6之后的.prop()和.attr()异同
- Jquery1.9 attr、prop的用法
- jquery1.6中的.prop()和.attr()异同
- jquery1.6中的.prop()和.attr()异同
- jquery1.6中的.prop()和.attr()异同
- jquery1.6中的.prop()和.attr()异同
- 委屈自己的常受委屈,会疼自己的才有人疼
- JQuery1.8的几个小变化
- $('#checkbox').attr('checked')的变化
- Linux-2.6.38后版本的变化
- attr在升级jquery1.9遇到的问题
- JQUERY1.6以后什么时候用attr什么时候用prop
- 灰常简易的顺序队列
- jquery1.9变化
- MAC OS ShortCut Key
- LVS在大规模网络环境中的应用
- 自己写Java NIO服务器实例
- 第三题
- 一个简洁的线程安全,支持按日期切分,级别设置的log类
- Jquery1.6版本后attr的变化,灰常委屈
- 《深入理解计算机系统》读后笔记一
- python 微信企业号
- (六十四)iOS的socket实现(C+OC混合实现)
- 查看Linux游戏服务器运行性能
- wp8开发者注册解锁手机IpOverUsbSvc错误
- 增强:通过程序查找CMOD出口
- 【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第十六课:Cocos2D-X几何图形1-3
- 幻灯片模式图片浏览器