jQuery源码解读之removeAttr()方法分析

  作者:bea

本文较为详细的分析了jQuery源码解读之removeAttr()方法。分享给大家供大家参考。具体分析如下: 扩展jQuery原型对象的方法: 代码如下: jQuery.fn.extend({ //name,传入要DOM元素要移除的属性名。 removeAttr: function( name ) { //使用jQuery.fn对象,即jQuery原型对象的each方法遍历当前选择器选择的jQuery对象数组,并返回该jQuery对象以便链式调用。 retur
本文较为详细的分析了jQuery源码解读之removeAttr()方法。分享给大家供大家参考。具体分析如下:
扩展jQuery原型对象的方法:

代码如下:


jQuery.fn.extend({
//name,传入要DOM元素要移除的属性名。
    removeAttr: function( name ) {

//使用jQuery.fn对象,即jQuery原型对象的each方法遍历当前选择器选择的jQuery对象数组,并返回该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+/g) //如果value为" ",则逻辑与表达式的值为null //如果value假设为"title href",则由于逻辑与操作符的两个操作数都不是布尔值,则返回第二个操作数,此时attrNames为["title", "href"]。 //match是JavaScript字符串的方法,在字符串内检索指定的值,或找到一个或多个正则表达式的匹配,返回存放匹配结果的数组。其他类型都会报错。             attrNames = value && value.match( rnotwhite ); //如果attrNames不为null,并且当前DOM对象的节点类型为1,进入if语句块,否则跳出函数,结束本次遍历,开始下次遍历。         if ( attrNames && elem.nodeType === 1 ) { //此时attrNames是个装有要移除属性名的数组,即["title", "href"] //执行while循环,这种写法的意思是,先从attrNames取出一个元素赋值给name, i自增1,然后判断name是否有值,有值,进入循环执行,执行完毕后开始下次循环,直到name无值,跳出循环。             while ( (name = attrNames[i++]) ) { //如果属性名与js关键字同名的如"for"和"class",替换为"htmlFor"和"className"。                 propName = jQuery.propFix[ name ] || name;
//如果是布尔值属性特别对待                 if ( jQuery.expr.match.bool.test( name ) ) { //getSetInput检测Input元素是否支持getAttribute("value") //getSetAttribute检测是否支持设置驼峰命名格式的属性名 //!ruseDefault.test( name )不区分大小写检测name是否是checked或者selected属性,                     if ( getSetInput && getSetAttribute || !ruseDefault.test( name ) ) { //移除布尔值属性实际上是给布尔值属性赋值为false                         elem[ propName ] = false;                     } else { //支持ie9以下 //将"default-checked"这种属性转换为"defaultChecked",并赋值false                         elem[ jQuery.camelCase( "default-" + name ) ] =                             elem[ propName ] = false;                     }                 } else { //如果不是布尔值属性,调用jQuery的全局attr方法设置属性                     jQuery.attr( elem, name, "" );                 } //getSetAttribute用来测试setAttribute是否支持设置驼峰命名形式的属性名,如果可以,在使用setAttribute和getAttribute时,需要使用修正后的属性名。(兼容ie6/7) //如果getSetAttibute等于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, fcamelCase );
    }
});
var nodeHook, boolHook,
    attrHandle = jQuery.expr.attrHandle,
    ruseDefault = /^(?:checked|selected)$/i,
    getSetAttribute = support.getSetAttribute,
    getSetInput = support.input;
// Setup
div = document.createElement( "div" );
div.setAttribute( "className", "t" );
div.innerHTML = "  <link/><table></table><a href='/a'>a</a><input type='checkbox'/>";
a = div.getElementsByTagName("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/setAttribute (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程序设计有所帮助。


有用  |  无用

猜你喜欢