午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    jQuery源碼解讀之removeAttr()方法分析
    來源:易賢網(wǎng) 閱讀:919 次 日期:2015-03-16 17:01:08
    溫馨提示:易賢網(wǎng)小編為您整理了“jQuery源碼解讀之removeAttr()方法分析”,方便廣大網(wǎng)友查閱!

    這篇文章主要介紹了jQuery源碼解讀之removeAttr()方法分析,較為詳細(xì)的分析了removeAttr方法的實(shí)現(xiàn)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

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

    擴(kuò)展jQuery原型對象的方法:

    代碼如下:

    jQuery.fn.extend({

    //name,傳入要DOM元素要移除的屬性名。

    removeAttr: function( name ) {

    //使用jQuery.fn對象,即jQuery原型對象的each方法遍歷當(dāng)前選擇器選擇的jQuery對象數(shù)組,并返回該jQuery對象以便鏈?zhǔn)秸{(diào)用。

    return this.each(function() {

    //調(diào)用jQuery的全局方法removeAttr,傳入遍歷出的DOM對象this和要移除的屬性名name。

    jQuery.removeAttr( this, name );

    });

    }

    });

    jQuery的全局方法removeAttr

    代碼如下:

    //擴(kuò)展jQuery對象的全局方法

    jQuery.extend({

    //elem為遍歷出的每個(gè)DOM對象,value為要移除的屬性名。

    removeAttr: function( elem, value ) {

    var name, propName,

    i = 0,

    //rnotwhite為(/\S+/g)

    //如果value為" ",則邏輯與表達(dá)式的值為null

    //如果value假設(shè)為"title href",則由于邏輯與操作符的兩個(gè)操作數(shù)都不是布爾值,則返回第二個(gè)操作數(shù),此時(shí)attrNames為["title", "href"]。

    //match是JavaScript字符串的方法,在字符串內(nèi)檢索指定的值,或找到一個(gè)或多個(gè)正則表達(dá)式的匹配,返回存放匹配結(jié)果的數(shù)組。其他類型都會報(bào)錯(cuò)。

    attrNames = value && value.match( rnotwhite );

    //如果attrNames不為null,并且當(dāng)前DOM對象的節(jié)點(diǎn)類型為1,進(jìn)入if語句塊,否則跳出函數(shù),結(jié)束本次遍歷,開始下次遍歷。

    if ( attrNames && elem.nodeType === 1 ) {

    //此時(shí)attrNames是個(gè)裝有要移除屬性名的數(shù)組,即["title", "href"]

    //執(zhí)行while循環(huán),這種寫法的意思是,先從attrNames取出一個(gè)元素賦值給name, i自增1,然后判斷name是否有值,有值,進(jìn)入循環(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 ) ) {

    //移除布爾值屬性實(shí)際上是給布爾值屬性賦值為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時(shí),需要使用修正后的屬性名。(兼容ie6/7)

    //如果getSetAttibute等于false,說明不支持,則使用修正后的屬性名,支持,使用原始的屬性名。

    //調(diào)用DOM原生的removeAttribute方法,移除屬性

    elem.removeAttribute( getSetAttribute ? name : propName );

    }

    }

    }

    });

    關(guān)鍵字屬性修正

    代碼如下:

    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程序設(shè)計(jì)有所幫助。

    更多信息請查看IT技術(shù)專欄

    更多信息請查看腳本欄目
    易賢網(wǎng)手機(jī)網(wǎng)站地址:jQuery源碼解讀之removeAttr()方法分析
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國考·省考課程試聽報(bào)名

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)