jQuery removeAttr() 源码解读
removeAttr比attr的代码要简单很多~~~
removeAttr: function( name ) {
return this.each(function() {
jQuery.removeAttr( this, name );
});
},
内部调用了jQuery.removeAttr方法,所以我们直接看它就可以啦~~
removeAttr: function( elem, value ) {
var name, propName,
i = 0,
//core_rnotwhite=/\S+/g
//value存在并且value可以匹配非空白字符
//这一步很帅的一点就是它不动声色地把多空格分隔的字符串转为数组,所以removeAttr是可以同时移除多个属性的
attrNames = value && value.match( core_rnotwhite );
if ( attrNames && elem.nodeType === 1 ) {//属性节点
while ( (name = attrNames[i++]) ) {
//propFix 属性修正
propName = jQuery.propFix[ name ] || name;
// Boolean attributes get special treatment (#10870)
//jQuery.expr.match.bool=/^(?:checked|selected|async|autofocus|autoplay|controls|defer|disabled|hidden|ismap|loop|multiple|open|readonly|required|scoped)$/i
if ( jQuery.expr.match.bool.test( name ) ) {
// Set corresponding property to false
elem[ propName ] = false;
}
elem.removeAttribute( name );
}
}
}
console一下jQuery.propFix,我们发现,它原来是这样一个对象:

cellpadding-->cellPadding ...
是对大小写的修正,都转为小驼峰法
class-->className for-->htmlFor
是考虑到js中关键字,所以将class映射到className ,js原生中就可以用element.getAttribute("className")
再看jQuery.expr.match.bool这个正则,它匹配的这些属性如checked、selected、async都是bool属性,jquery为什么要特别加一句
elem[ propName ] = false;呢? 因为对于低版本的IE(6、7)来说,单单removeAttribute并不能移除bool属性。不加这一句,我们$().attr("checked")的时候,还是会返回“checked”。
测试如下:
DEMO1
<body>
<input id="ck" type="checkbox" checked>
<script type="text/javascript">
var ck=document.getElementById('ck');
//ck.checked=false;
ck.removeAttribute('checked');
alert(ck.getAttribute("checked"));
</scrip

DEMO2
<body>
<input id="ck" type="checkbox" checked>
<script type="text/javascript">
var ck=document.getElementById('ck');
ck.checked=false;
ck.removeAttribute('checked');
alert(ck.getAttribute("checked"));
</script>
</body>

jQuery removeAttr() 源码解读的更多相关文章
- jQuery.Callbacks 源码解读二
一.参数标记 /* * once: 确保回调列表仅只fire一次 * unique: 在执行add操作中,确保回调列表中不存在重复的回调 * stopOnFalse: 当执行回调返回值为false,则 ...
- jQuery attr() 源码解读
我们知道,$().attr()实质上是内部调用了jQuery.access方法,在调用时jQuery.attr作为回调传入.在通过种种判断(参看jQuery.access()方法)之后,取值和赋值最后 ...
- jQuery toggleClass 源码解读
toggleClass: function( value, stateVal ) { var type = typeof value;//值类型 if ( typeof stateVal === &q ...
- jquery.fileupload源码解读笔记
基础编程风格 新建 test.html 和 test.js和 main.js和 无论哪种顺序 <body> <script src="/Sandeep/js/jquery ...
- jQuery.extend()源码解读
// extend方法为jQuery对象和init对象的prototype扩展方法// 同时具有独立的扩展普通对象的功能jQuery.extend = jQuery.fn.extend = funct ...
- jQuery框架源码解读
1.jQuery 1.9.1 parseJSON: function( data ) { // Attempt to parse using the native JSON parser first ...
- jQuery position() 源码解读
position的代码比较简单... position: function() { if ( !this[ 0 ] ) { return; } var offsetParent, offset, el ...
- jquery offsetParent()源码解读
offsetParent: function() { return this.map(function() { var offsetParent = this.offsetParent || docE ...
- jQuery addClass() 源码解读
addClass: function( value ) { var classes, elem, cur, clazz, j, i = 0, len = this.length, proceed = ...
随机推荐
- BZOJ 2115: [Wc2011] Xor DFS + 线性基
2115: [Wc2011] Xor Time Limit: 10 Sec Memory Limit: 259 MB Description Input 第一行包含两个整数N和 M, 表示该无向图中 ...
- SLG, 菱形格子的算法.(递归版
class GeoPoint{ public: int x; int y; public: bool operator == (const GeoPoint& p){ return p.x = ...
- java 获取项目根目录
代码入下: request.getSession().getServletContext().getRealPath(); 这里的getRealPath()括号里面可以输入你想得到的具体目录. 需要注 ...
- 解决virtualbox安装增强工具失败的问题
virtualbox有个增强工具,安装之后用户体验是非常爽的.但是有些时候在安装增强工具会遇到一些小问题,无非是没有安装gcc,make之类的编译工具或是需要安装kernel*.而我遇到的问题在做了这 ...
- File.Copy的时候Could not find a part of the path
https://developercommunity.visualstudio.com/content/problem/378265/filecopy-did-not-throw-the-correc ...
- HashSe、LinkedHashSet、TreeSet(java基础知识十七)
1.HashSet存储字符串并遍历 * 特点:无序.无索引.无重复 HashSet存储字符串并遍历 HashSet<String> hs = new HashSet<>(); ...
- bzoj4516
后缀自动机 留个板子 upd:大概懂了 每次新加入的npRight集合肯定只有最后一个位置,那么求所有长得不一样的子串贡献就是Max-Min+1,因为Right集合只有这一个位置,所以这Max-Min ...
- JSP有哪些内置对象
JSP有哪些内置对象? 1.page:JSP网页本身; 2.request:用户端请求,此请求会包含来自GET/POST请求的参数; 3.session:请求有关的会话; 4.application: ...
- A. Hulk
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- centos安装xen虚拟机并且配置bridge
主要参考的几个官方文档: http://wiki.centos.org/HowTos/Xen/Xen4QuickStart 在centos上安装xen组件并建立dom0 http://wiki.cen ...