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 = ...
随机推荐
- easyui tree的简单使用
Tree 数据转换 所有节点都包含以下属性: id:节点id,这个很重要到加载远程服务器数据 which is important to load remote data text: 显示的节点文本 ...
- Android开发之开机自动启动应用
package com.raycloud.wolf.autostart; import android.content.BroadcastReceiver; import android.conten ...
- YTU 2421: C语言习题 矩形法求定积分
2421: C语言习题 矩形法求定积分 时间限制: 1 Sec 内存限制: 128 MB 提交: 354 解决: 234 题目描述 写一个用矩形法求定积分的通用函数,分别求 (说明: sin,co ...
- 织梦CMS如何在首页调用指定的文章 idlist
在网站首页调用站内新闻是必不可少的,但是有的时候不能根据自己的需要来调用指定的文章,想要调用自己指定的文章还要做一些修改. 在网站中调用指定文章可以使用织梦默认的标签idlist,在调用的时候使用以下 ...
- box-sizing: border-box;的作用
box-sizing 属性可以被用来调整这些表现: content-box 是默认值.如果你设置一个元素的宽为100px,那么这个元素的内容区会有100px宽,并且任何边框和内边距的宽度都会被增加到 ...
- 解决VMware安装Ubuntu的过程中窗口过小无法看到【下一步】按钮的问题
只要按住ALT键向上拖动窗口
- Web Assembly背景
Javascript ,也叫Ecma script, 是这家伙用了 10 天时间赶出来的.. 所以,各位程序猿们,如果你觉得老板 10 天要你们上线一个 App 是一个丧心病狂的事情,那么可以多想想 ...
- Redhat 安装perl模块
CPAN上下载要安装的模块 解压 gzip -d DBD-mysql-4.006.tar.gz tar xvf DBD-mysql-4.006.tar 然后进入DBD-mysql-4.006目录,执行 ...
- BZOJ_1492_[NOI2007]货币兑换Cash_CDQ分治+斜率优化
BZOJ_1492_[NOI2007]货币兑换Cash_CDQ分治+斜率优化 Description 小Y最近在一家金券交易所工作.该金券交易所只发行交易两种金券:A纪念券(以下简称A券)和 B纪念券 ...
- javascript之常遇到的浏览器兼容问题和解决方法
转自http://www.cnblogs.com/duenyang/p/6066737.html 常遇到的关于浏览器的宽高问题: var winW=document.body.clientWidth| ...