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

JS中attr和prop屬性的區(qū)別以及優(yōu)先選擇示例介紹

 更新時(shí)間:2014年06月30日 17:50:31   投稿:whsnow  
這篇文章主要介紹了JS中attr和prop屬性的區(qū)別以及優(yōu)先選擇,需要的朋友可以參考下

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

既然我們想知道他們兩的區(qū)別,最好就看看他們的源代碼,不要被代碼長度所嚇到,我們只看關(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 ),很明顯的看出來,使用的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對象的一個(gè)屬性。

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

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

$(function(){ 
el = $("#test"); 
console.log(el.attr("style")); //undefined 
console.log(el.prop("style")); //CSSStyleDeclaration對象 
console.log(document.getElementById("test").style); //CSSStyleDeclaration對象 
});

el.attr(“style”)輸出undefined,因?yàn)閍ttr是獲取的這個(gè)對象屬性節(jié)點(diǎn)的值,很顯然此時(shí)沒有這個(gè)屬性節(jié)點(diǎn),自然輸出undefined
el.prop(“style”)輸出CSSStyleDeclaration對象,對于一個(gè)DOM對象,是具有原生的style對象屬性的,所以輸出了style對象
至于document.getElementById(“test”).style和上面那條一樣

接著看:

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


首先用attr方法給這個(gè)對象添加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

我們再用prop方法給這個(gè)對象設(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


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

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”來獲取這些屬性,就是來解決這個(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升級的時(shí)候大家要注意這點(diǎn)!

以下是官方建議attr(),prop()的使用:

以下是官方建議attr(),prop()的使用:

相關(guān)文章

  • Dojo Javascript 編程規(guī)范 規(guī)范自己的JavaScript書寫

    Dojo Javascript 編程規(guī)范 規(guī)范自己的JavaScript書寫

    良好的JavaScript書寫習(xí)慣的優(yōu)點(diǎn)不言而喻,今天彬Go向大家推薦Dojo Javascript 編程規(guī)范,相當(dāng)不錯(cuò)的 Javascript 編程風(fēng)格規(guī)范,建議大家可以借鑒一下此規(guī)范編寫 Javascript。感謝i.feelinglucky的翻譯
    2014-10-10
  • 詳細(xì)分析JS函數(shù)去抖和節(jié)流

    詳細(xì)分析JS函數(shù)去抖和節(jié)流

    這篇文章主要介紹了詳細(xì)分析JS函數(shù)去抖和節(jié)流相關(guān)知識以及代碼分析,需要的朋友學(xué)習(xí)參考下吧。
    2017-12-12
  • JavaScript操作Cookie詳解

    JavaScript操作Cookie詳解

    這篇文章主要介紹了JavaScript操作Cookie詳解,本文講解了什么是Cookie、Cookie基礎(chǔ)知識、Cookie常見問題、cookie 有兩種清除方式、Cookie基礎(chǔ)用法、Cookie高級用法等內(nèi)容,需要的朋友可以參考下
    2015-02-02
  • window.parent與window.openner區(qū)別介紹

    window.parent與window.openner區(qū)別介紹

    今天總結(jié)一下js中幾個(gè)對象的區(qū)別和用法,對這幾個(gè)概念混淆的朋友可以看看
    2012-04-04
  • JavaScript 入門·JavaScript 具有全范圍的運(yùn)算符

    JavaScript 入門·JavaScript 具有全范圍的運(yùn)算符

    JavaScript 入門·JavaScript 具有全范圍的運(yùn)算符...
    2007-10-10
  • 深入理解JS中的Function.prototype.bind()方法

    深入理解JS中的Function.prototype.bind()方法

    bind 是 ES5 中新增的一個(gè)方法,可以改變函數(shù)內(nèi)部的this指向。這篇文章小編將帶領(lǐng)大家深入理解Javascript中的Function.prototype.bind()方法。有需要的朋友們可以參考借鑒,下面來一起看看吧。
    2016-10-10
  • Javascript中的五種數(shù)據(jù)類型詳解

    Javascript中的五種數(shù)據(jù)類型詳解

    這篇文章主要介紹了Javascript中的五種數(shù)據(jù)類型詳解,需要的朋友可以參考下
    2014-12-12
  • javascript數(shù)據(jù)代理與事件詳解分析

    javascript數(shù)據(jù)代理與事件詳解分析

    所謂數(shù)據(jù)代理(也叫數(shù)據(jù)劫持),指的是在訪問或者修改對象的某個(gè)屬性時(shí),通過一段代碼攔截這個(gè)行為,進(jìn)行額外的操作或者修改返回結(jié)果。比較典型的是 Object.defineProperty() 和 ES2015 中新增的 Proxy 對象
    2021-11-11
  • javascript日期格式化示例分享

    javascript日期格式化示例分享

    這篇文章主要介紹了javascript日期格式化示例,需要的朋友可以參考下
    2014-03-03
  • 完全不用基礎(chǔ)的HTML5入門篇教程

    完全不用基礎(chǔ)的HTML5入門篇教程

    HTML的全稱為超文本標(biāo)記語言,是一種標(biāo)記語言。它包括一系列標(biāo)簽.通過這些標(biāo)簽可以將網(wǎng)絡(luò)上的文檔格式統(tǒng)一,使分散的Internet資源連接為一個(gè)邏輯整體。HTML文本是由HTML命令組成的描述性文本,HTML命令可以說明文字,圖形、動(dòng)畫、聲音、表格、鏈接等
    2022-03-03

最新評論

怀安县| 峨山| 晋宁县| 青海省| 分宜县| 平远县| 瓦房店市| 临清市| 乌什县| 镇赉县| 河池市| 军事| 舞钢市| 罗田县| 阳江市| 双牌县| 拜城县| 白银市| 绍兴市| 凤阳县| 庆阳市| 五寨县| 米泉市| 郸城县| 德兴市| 洪洞县| 喀什市| 当阳市| 江达县| 辉南县| 上思县| 澄城县| 略阳县| 宜州市| 琼中| 梓潼县| 和林格尔县| 奉节县| 出国| 敦煌市| 台北县|