jquery源码解读之removeattr 方法分析

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

《jquery源码解读之removeattr 方法分析》由会员分享,可在线阅读,更多相关《jquery源码解读之removeattr 方法分析(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号