jquery源码解读之removeattr 方法分析_1

上传人:F****n 文档编号:69362548 上传时间:2019-01-13 格式:DOCX 页数:7 大小:20.74KB
返回 下载 相关 举报
jquery源码解读之removeattr 方法分析_1_第1页
第1页 / 共7页
jquery源码解读之removeattr 方法分析_1_第2页
第2页 / 共7页
jquery源码解读之removeattr 方法分析_1_第3页
第3页 / 共7页
jquery源码解读之removeattr 方法分析_1_第4页
第4页 / 共7页
jquery源码解读之removeattr 方法分析_1_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《jquery源码解读之removeattr 方法分析_1》由会员分享,可在线阅读,更多相关《jquery源码解读之removeattr 方法分析_1(7页珍藏版)》请在金锄头文库上搜索。

1、jQuery源码解读之removeAttr 方法分析这篇文章主要介绍了jQuery源码解读之removeAttr 方法分析,较为详细的分析了removeAttr方法的实现技巧,非常具有实用价值,需要的朋友可以参考下本文较为详细的分析了jQuery源码解读之removeAttr 方法。分享给大家供大家参考。具体分析如下:扩展jQuery原型对象的方法:代码如下:jQuery.fn.extend(/name,传入要DOM元素要移除的属性名。removeAttr: function( name ) /使用jQuery.fn对象,即jQuery原型对象的each方法遍历当前选择器选择的jQuery对象

2、数组,并返回该jQuery对象以便链式调用。return this.each(function /调用jQuery的全局方法removeAttr,传入遍历出的DOM对象this和要移除的属性名name。jQuery.removeAttr( this, name );););jQuery的全局方法removeAttr代码如下:/扩展jQuery对象的全局方法jQuery.extend(/elem为遍历出的每个DOM对象,value为要移除的属性名。removeAttr: function( elem, value ) var name, propName,i = 0,/rnotwhite为(/S

3、+/g)/如果value为 ,则逻辑与表达式的值为null/如果value假设为title href,则由于逻辑与操作符的两个操作数都不是布尔值,则返回第二个操作数,此时attrNames为title, href。/match是JavaScript字符串的方法,在字符串内检索指定的值,或找到一个或多个正则表达式的匹配,返回存放匹配结果的数组。其他类型都会报错。attrNames = value & value.match( rnotwhite );/如果attrNames不为null,并且当前DOM对象的节点类型为1,进入if语句块,否则跳出函数,结束本次遍历,开始下次遍历。if ( attr

4、Names & elem.nodeType = 1 ) /此时attrNames是个装有要移除属性名的数组,即title, href/执行while循环,这种写法的意思是,先从attrNames取出一个元素赋值给name, i自增1,然后判断name是否有值,有值,进入循环执行,执行完毕后开始下次循环,直到name无值,跳出循环。while ( (name = attrNamesi+) ) /如果属性名与js关键字同名的如for和class,替换为htmlFor和className。propName = jQuery.propFix name | name;/如果是布尔值属性特别对待if (

5、jQuery.expr.match.bool.test( name ) ) /getSetInput检测Input元素是否支持getAttribute(value)/getSetAttribute检测是否支持设置驼峰命名格式的属性名/!ruseDefault.test( name )不区分大小写检测name是否是checked或者selected属性,if ( getSetInput & getSetAttribute | !ruseDefault.test( name ) ) /移除布尔值属性实际上是给布尔值属性赋值为falseelem propName = false; else /支持i

6、e9以下/将default-checked这种属性转换为defaultChecked,并赋值falseelem jQuery.camelCase( default- + name ) =elem propName = false; else /如果不是布尔值属性,调用jQuery的全局attr方法设置属性jQuery.attr( elem, name, );/getSetAttribute用来测试setAttribute是否支持设置驼峰命名形式的属性名,如果可以,在使用setAttribute和getAttribute时,需要使用修正后的属性名。(兼容ie6/7)/如果getSetAttibu

7、te等于false,说明不支持,则使用修正后的属性名,支持,使用原始的属性名。/调用DOM原生的removeAttribute方法,移除属性elem.removeAttribute( getSetAttribute ? name : propName ););关键字属性修正代码如下:jQuery.extend(propFix: for: htmlFor,class: className);jQuery.extend(camelCase: function( string ) return string.replace( rmsPrefix, ms- ).replace( rdashAlpha,

8、 fcamelCase ););var nodeHook, boolHook,attrHandle = jQuery.expr.attrHandle,ruseDefault = /(?:checked|selected)$/i,getSetAttribute = support.getSetAttribute,getSetInput = support.input;/ Setupdiv = document.createElement( div );div.setAttribute( className, t );div.innerHTML = a;a = div.getElementsByT

9、agName(a) 0 ;/ First batch of tests.select = document.createElement(select);opt = select.appendChild( document.createElement(option) );input = div.getElementsByTagName(input) 0 ;a.style.cssText = top:1px;/ Test setAttribute on camelCase class. If it works, we need attrFixes when doing get/setAttribu

10、te (ie6/7)support.getSetAttribute = div.className != t;检测input是否支持getAttribute(value)代码如下:/ Support: IE8 only/ Check if we can trust getAttribute(value)input = document.createElement( input );input.setAttribute( value, );support.input = input.getAttribute( value ) = ;检测是否布尔值属性代码如下:booleans = checked|selected|async|autofocus|autoplay|controls|defer|disabled|hidden|ismap|loop|multiple|open|readonly|required|scoped,matchExpr = bool: new RegExp( (?: + booleans + )$, i ),希望本文所述对大家的jQuery程序设计有所帮助。

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 事务文书

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号