最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jQuery源碼解讀之removeAttr()方法分析

 更新時間:2015年02月20日 11:19:32   作者:novaline  
這篇文章主要介紹了jQuery源碼解讀之removeAttr()方法分析,較為詳細的分析了removeAttr方法的實現(xiàn)技巧,非常具有實用價值,需要的朋友可以參考下

本文較為詳細的分析了jQuery源碼解讀之removeAttr()方法。分享給大家供大家參考。具體分析如下:

擴展jQuery原型對象的方法:

復(fù)制代碼 代碼如下:
jQuery.fn.extend({
//name,傳入要DOM元素要移除的屬性名。
    removeAttr: function( name ) {

//使用jQuery.fn對象,即jQuery原型對象的each方法遍歷當(dāng)前選擇器選擇的jQuery對象數(shù)組,并返回該jQuery對象以便鏈式調(diào)用。
        return this.each(function() {
//調(diào)用jQuery的全局方法removeAttr,傳入遍歷出的DOM對象this和要移除的屬性名name。
            jQuery.removeAttr( this, name );
        });
    }
});

jQuery的全局方法removeAttr

復(fù)制代碼 代碼如下:
//擴展jQuery對象的全局方法
jQuery.extend({

//elem為遍歷出的每個DOM對象,value為要移除的屬性名。
    removeAttr: function( elem, value ) {
        var name, propName,
            i = 0,
//rnotwhite為(/\S+/g)
//如果value為" ",則邏輯與表達式的值為null
//如果value假設(shè)為"title href",則由于邏輯與操作符的兩個操作數(shù)都不是布爾值,則返回第二個操作數(shù),此時attrNames為["title", "href"]。
//match是JavaScript字符串的方法,在字符串內(nèi)檢索指定的值,或找到一個或多個正則表達式的匹配,返回存放匹配結(jié)果的數(shù)組。其他類型都會報錯。
            attrNames = value && value.match( rnotwhite );
//如果attrNames不為null,并且當(dāng)前DOM對象的節(jié)點類型為1,進入if語句塊,否則跳出函數(shù),結(jié)束本次遍歷,開始下次遍歷。
        if ( attrNames && elem.nodeType === 1 ) {
//此時attrNames是個裝有要移除屬性名的數(shù)組,即["title", "href"]
//執(zhí)行while循環(huán),這種寫法的意思是,先從attrNames取出一個元素賦值給name, i自增1,然后判斷name是否有值,有值,進入循環(huán)執(zhí)行,執(zhí)行完畢后開始下次循環(huán),直到name無值,跳出循環(huán)。
            while ( (name = attrNames[i++]) ) {
//如果屬性名與js關(guān)鍵字同名的如"for"和"class",替換為"htmlFor"和"className"。
                propName = jQuery.propFix[ name ] || name;

//如果是布爾值屬性特別對待
                if ( jQuery.expr.match.bool.test( name ) ) {
//getSetInput檢測Input元素是否支持getAttribute("value")
//getSetAttribute檢測是否支持設(shè)置駝峰命名格式的屬性名
//!ruseDefault.test( name )不區(qū)分大小寫檢測name是否是checked或者selected屬性,
                    if ( getSetInput && getSetAttribute || !ruseDefault.test( name ) ) {
//移除布爾值屬性實際上是給布爾值屬性賦值為false
                        elem[ propName ] = false;
                    } else {
//支持ie9以下
//將"default-checked"這種屬性轉(zhuǎn)換為"defaultChecked",并賦值false
                        elem[ jQuery.camelCase( "default-" + name ) ] =
                            elem[ propName ] = false;
                    }
                } else {
//如果不是布爾值屬性,調(diào)用jQuery的全局attr方法設(shè)置屬性
                    jQuery.attr( elem, name, "" );
                }
//getSetAttribute用來測試setAttribute是否支持設(shè)置駝峰命名形式的屬性名,如果可以,在使用setAttribute和getAttribute時,需要使用修正后的屬性名。(兼容ie6/7)
//如果getSetAttibute等于false,說明不支持,則使用修正后的屬性名,支持,使用原始的屬性名。
//調(diào)用DOM原生的removeAttribute方法,移除屬性
                elem.removeAttribute( getSetAttribute ? name : propName );
            }
        }
    }
});


關(guān)鍵字屬性修正
復(fù)制代碼 代碼如下:
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")

復(fù)制代碼 代碼如下:
// Support: IE8 only
// Check if we can trust getAttribute("value")
input = document.createElement( "input" );
input.setAttribute( "value", "" );
support.input = input.getAttribute( "value" ) === "";

檢測是否布爾值屬性

復(fù)制代碼 代碼如下:
booleans = "checked|selected|async|autofocus|autoplay|controls|defer|disabled|hidden|ismap|loop|multiple|open|readonly|required|scoped",

matchExpr = {
    "bool": new RegExp( "^(?:" + booleans + ")$", "i" )
},

希望本文所述對大家的jQuery程序設(shè)計有所幫助。

相關(guān)文章

最新評論

桃源县| 铁岭市| 双桥区| 新绛县| 台湾省| 柘城县| 建瓯市| 来安县| 西丰县| 双柏县| 宜宾市| 多伦县| 大邑县| 七台河市| 如东县| 岳池县| 兰西县| 五指山市| 安塞县| 玉龙| 西吉县| 新乡县| 抚远县| 六盘水市| 许昌县| 固安县| 彰化县| 淄博市| 交口县| 大庆市| 漾濞| 宜丰县| 云阳县| 凌云县| 星座| 榆中县| 吴川市| 弥勒县| 庆元县| 张家口市| 长丰县|