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 = ...
随机推荐
- hibernate面试点
1.谈谈你对hibernate的认识和理解 01.全自动的ORM框架 02.子项目 03.面向对象的思想来解决操作数据库 01.hibernate是一个开放源代码的对象关系映射(ORM)框架,它对JD ...
- 使用Java绘制验证码
效果图: JDemo.java import java.io.File; import java.io.IOException; import static java.lang.System.out; ...
- Form Template Method
<重构>中此方法叫做塑造模板函数,在设计模式中,对应的模式就是模板模式.重构中的很多变动比较大的方法都会导致重构,但重构中有非常多的小重构手法.就好像建筑一个房子,设计模式教你厨房客厅怎么 ...
- kafka2
Master-Slave: 读写分离,save复制master的数据.同步复制:保证了强一致性但是会影响高可用性,因为写入的时候要保证slave都写入了才能返回告诉生产者数据写入成功,如果slave过 ...
- Healthy Holsteins
链接 分析:因为数据范围比较小,我们可以通过二进制枚举子集,然后找出所需饲料种数最小的并记录下来,同时记录一下路径,也就是字典序最小的 /* PROB:holstein ID:wanghan LANG ...
- MyBatis相关资源
MyBatis很多项目中有用到,但会用并不表示你真正理解它,更不代表你能很清楚的教会别人.如果想在会用它的基础上更深入的通过学习它而提升自己技术能力,可利用下面资源. 1.官网文档,基本概念讲的很清楚 ...
- Linux环境下在Tomcat上部署JavaWeb工程
本文讲解如何将我们已经编译好的JavaWeb工程在Linux环境下的Tomcat上进行部署,总体上的思路是和Windows下JavaWeb项目部署到tomcat差不多,具体步骤和命令如下. 注:部署之 ...
- JS两个数组比较,删除重复值巧妙方法
//方法一 var arr1 = [1,2,3,4,5,6,7,8]; //数组A var arr2 = [1,2,3,11,12,13,14];//数组B var temp = []; //临时数组 ...
- View Controller Programming Guide for iOS---(七)---Resizing the View Controller’s Views
Resizing the View Controller’s Views A view controller owns its own view and manages the view’s cont ...
- django上课笔记1-目录介绍-路由系统-ORM操作
一.Django目录介绍 django-admin startproject mysite # 创建名为mysite的项目 cd mysite # 切换到该目录下 python manage.py s ...