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

    JS中attr和prop屬性的區(qū)別以及優(yōu)先選擇示例
    來(lái)源:易賢網(wǎng) 閱讀:675 次 日期:2014-09-11 11:53:09
    溫馨提示:易賢網(wǎng)小編為您整理了“JS中attr和prop屬性的區(qū)別以及優(yōu)先選擇示例”,方便廣大網(wǎng)友查閱!

    相比attr,prop是1.6.1才新出來(lái)的,兩者從中文意思理解,都是獲取/設(shè)置屬性的方法(attributes和properties)。只是,window或document中使用.attr()方法在jQuery1.6之前不能正常運(yùn)行,因?yàn)閣indow和document中不能有attributes。prop應(yīng)運(yùn)而生了。

    既然我們想知道他們兩的區(qū)別,最好就看看他們的源代碼,不要被代碼長(zhǎng)度所嚇到,我們只看關(guān)鍵的幾句:

    attr: function( elem, name, value, pass ) {

    var ret, hooks, notxml,

    nType = elem.nodeType;

    // don't get/set attributes on text, comment and attribute nodes

    if ( !elem || nType === 3 || nType === 8 || nType === 2 ) {

    return;

    }

    if ( pass && jQuery.isFunction( jQuery.fn[ name ] ) ) {

    return jQuery( elem )[ name ]( value );

    }

    // Fallback to prop when attributes are not supported

    if ( typeof elem.getAttribute === "undefined" ) {

    return jQuery.prop( elem, name, value );

    }

    notxml = nType !== 1 || !jQuery.isXMLDoc( elem );

    // All attributes are lowercase

    // Grab necessary hook if one is defined

    if ( notxml ) {

    name = name.toLowerCase();

    hooks = jQuery.attrHooks[ name ] || ( rboolean.test( name ) ? boolHook : nodeHook );

    }

    if ( value !== undefined ) {

    if ( value === null ) {

    jQuery.removeAttr( elem, name );

    return;

    } else if ( hooks && "set" in hooks && notxml && (ret = hooks.set( elem, value, name )) !== undefined ) {

    return ret;

    } else {

    elem.setAttribute( name, value + "" );

    return value;

    }

    } else if ( hooks && "get" in hooks && notxml && (ret = hooks.get( elem, name )) !== null ) {

    return ret;

    } else {

    ret = elem.getAttribute( name );

    // Non-existent attributes return null, we normalize to undefined

    return ret === null ?

    undefined :

    ret;

    }

    }

    prop方法代碼(jQuery版本1.8.3)

    prop: function( elem, name, value ) {

    var ret, hooks, notxml,

    nType = elem.nodeType;

    // don't get/set properties on text, comment and attribute nodes

    if ( !elem || nType === 3 || nType === 8 || nType === 2 ) {

    return;

    }

    notxml = nType !== 1 || !jQuery.isXMLDoc( elem );

    if ( notxml ) {

    // Fix name and attach hooks

    name = jQuery.propFix[ name ] || name;

    hooks = jQuery.propHooks[ name ];

    }

    if ( value !== undefined ) {

    if ( hooks && "set" in hooks && (ret = hooks.set( elem, value, name )) !== undefined ) {

    return ret;

    } else {

    return ( elem[ name ] = value );

    }

    } else {

    if ( hooks && "get" in hooks && (ret = hooks.get( elem, name )) !== null ) {

    return ret;

    } else {

    return elem[ name ];

    }

    }

    }

    attr方法里面,最關(guān)鍵的兩行代碼,elem.setAttribute( name, value + “” )和ret = elem.getAttribute( name ),很明顯的看出來(lái),使用的DOM的API setAttribute和getAttribute方法操作的屬性元素節(jié)點(diǎn)。

    而prop方法里面,最關(guān)鍵的兩行代碼,return ( elem[ name ] = value )和return elem[ name ],你可以理解成這樣document.getElementById(el)[name] = value,這是轉(zhuǎn)化成JS對(duì)象的一個(gè)屬性。

    既然明白了原理是這樣,我們來(lái)看看一個(gè)例子:

    <input type="checkbox" id="test" abc="111" />

    $(function(){

    el = $("#test");

    console.log(el.attr("style")); //undefined

    console.log(el.prop("style")); //CSSStyleDeclaration對(duì)象

    console.log(document.getElementById("test").style); //CSSStyleDeclaration對(duì)象

    });

    el.attr(“style”)輸出undefined,因?yàn)閍ttr是獲取的這個(gè)對(duì)象屬性節(jié)點(diǎn)的值,很顯然此時(shí)沒有這個(gè)屬性節(jié)點(diǎn),自然輸出undefined

    el.prop(“style”)輸出CSSStyleDeclaration對(duì)象,對(duì)于一個(gè)DOM對(duì)象,是具有原生的style對(duì)象屬性的,所以輸出了style對(duì)象

    至于document.getElementById(“test”).style和上面那條一樣

    接著看:

    el.attr("abc","111")

    console.log(el.attr("abc")); //111

    console.log(el.prop("abc")); //undefined

    首先用attr方法給這個(gè)對(duì)象添加abc節(jié)點(diǎn)屬性,值為111,可以看到html的結(jié)構(gòu)也變了

    el.attr(“abc”)輸出結(jié)果為111,再正常不過了

    el.prop(“abc”)輸出undefined,因?yàn)閍bc是在這個(gè)的屬性節(jié)點(diǎn)中,所以通過prop是取不到的

    el.prop("abc", "222");

    console.log(el.attr("abc")); //111

    console.log(el.prop("abc")); //222

    我們?cè)儆胮rop方法給這個(gè)對(duì)象設(shè)置了abc屬性,值為222,可以看到html的結(jié)構(gòu)是沒有變化的。輸出的結(jié)果就不解釋了。

    上面已經(jīng)把原理講清楚了,什么時(shí)候用什么就可以自己把握了。

    提一下,在遇到要獲取或設(shè)置checked,selected,readonly和disabled等屬性時(shí),用prop方法顯然更好,比如像下面這樣:

    <input type="checkbox" id="test" checked="checked" />

    console.log(el.attr("checked")); //checked

    console.log(el.prop("checked")); //true

    console.log(el.attr("disabled")); //undefined

    console.log(el.prop("disabled")); //false

    顯然,布爾值比字符串值讓接下來(lái)的處理更合理。

    PS一下,如果你有JS性能潔癖的話,顯然prop的性能更高,因?yàn)閍ttr需要訪問DOM屬性節(jié)點(diǎn),訪問DOM是最耗時(shí)的。這種情況適用于多選項(xiàng)全選和反選的情況。

    大家都知道有的瀏覽器只要寫disabled,checked就可以了,而有的要寫成disabled = "disabled",checked="checked",比如用attr("checked")獲取checkbox的checked屬性時(shí)選中的時(shí)候可以取到值,值為"checked"但沒選中獲取值就是undefined。

    jq提供新的方法“prop”來(lái)獲取這些屬性,就是來(lái)解決這個(gè)問題的,以前我們使用attr獲取checked屬性時(shí)返回"checked"和"",現(xiàn)在使用prop方法獲取屬性則統(tǒng)一返回true和false。

    那么,什么時(shí)候使用attr(),什么時(shí)候使用prop()?

    1.添加屬性名稱該屬性就會(huì)生效應(yīng)該使用prop();

    2.是有true,false兩個(gè)屬性使用prop();

    3.其他則使用attr();

    項(xiàng)目中jquery升級(jí)的時(shí)候大家要注意這點(diǎn)!

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

    更多信息請(qǐng)查看腳本欄目
    易賢網(wǎng)手機(jī)網(wǎng)站地址:JS中attr和prop屬性的區(qū)別以及優(yōu)先選擇示例
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

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

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