欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

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方法里面,最關鍵的兩行代碼,elem.setAttribute( name, value + “” )和ret = elem.getAttribute( name ),很明顯的看出來,使用的DOM的API setAttribute和getAttribute方法操作的屬性元素節(jié)點。
而prop方法里面,最關鍵的兩行代碼,return ( elem[ name ] = value )和return elem[ name ],你可以理解成這樣document.getElementById(el)[name] = value,這是轉化成JS對象的一個屬性。

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

<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,因為attr是獲取的這個對象屬性節(jié)點的值,很顯然此時沒有這個屬性節(jié)點,自然輸出undefined
el.prop(“style”)輸出CSSStyleDeclaration對象,對于一個DOM對象,是具有原生的style對象屬性的,所以輸出了style對象
至于document.getElementById(“test”).style和上面那條一樣

接著看:

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


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

el.attr(“abc”)輸出結果為111,再正常不過了
el.prop(“abc”)輸出undefined,因為abc是在這個的屬性節(jié)點中,所以通過prop是取不到的

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

我們再用prop方法給這個對象設置了abc屬性,值為222,可以看到html的結構是沒有變化的。輸出的結果就不解釋了。

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

提一下,在遇到要獲取或設置checked,selected,readonly和disabled等屬性時,用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的性能更高,因為attr需要訪問DOM屬性節(jié)點,訪問DOM是最耗時的。這種情況適用于多選項全選和反選的情況。

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

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

那么,什么時候使用attr(),什么時候使用prop()?
1.添加屬性名稱該屬性就會生效應該使用prop();
2.是有true,false兩個屬性使用prop();
3.其他則使用attr();
項目中jquery升級的時候大家要注意這點!

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

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

相關文章

  • js css后面所帶參數(shù)含義介紹

    js css后面所帶參數(shù)含義介紹

    網(wǎng)頁鏈接的js css后面總是帶有一些參數(shù),想必大家并不陌生,下面為大家介紹下這些參數(shù)的含義,感興趣的朋友可以參考下
    2013-08-08
  • javaScript基礎語法介紹

    javaScript基礎語法介紹

    本文從javascript簡介開始,介紹了javascript的語法以及注意事項、動態(tài)語言、引用外部JS文件、變量命名規(guī)則、判斷是否已經(jīng)聲明、不存在塊級作用域這些方面的內(nèi)容,是篇相當不錯的基礎語法的介紹文章,推薦給小伙伴們
    2015-02-02
  • JavaScript之數(shù)組扁平化詳解

    JavaScript之數(shù)組扁平化詳解

    這篇文章主要介紹了JavaScript之數(shù)組扁平化詳解的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,,需要的朋友可以參考下
    2019-06-06
  • javascript類型系統(tǒng)——undefined和null全面了解

    javascript類型系統(tǒng)——undefined和null全面了解

    下面小編就為大家?guī)硪黄猨avascript類型系統(tǒng)——undefined和null全面了解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-07-07
  • 淺談一下forward和redirect的區(qū)別

    淺談一下forward和redirect的區(qū)別

    這篇文章主要介紹了forward和redirect的區(qū)別,在前端開發(fā)中,有很多剛入行的朋友不了解這兩者之間的區(qū)別,那么這篇文章可以帶給你新的認知,需要的朋友可以參考下
    2023-03-03
  • 快速解決Canvas.toDataURL 圖片跨域的問題

    快速解決Canvas.toDataURL 圖片跨域的問題

    出現(xiàn)Canvas.toDataURL 圖片跨域問題怎么解決呢?下面小編就為大家?guī)硪黄狢anvas.toDataURL 圖片跨域問題的快速解決方法。一起跟隨小編過來看看吧
    2016-05-05
  • 淺談JavaScript 標準對象

    淺談JavaScript 標準對象

    下面小編就為大家?guī)硪黄獪\談JavaScript 標準對象。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • JavaScript link方法入門實例(給字符串加上超鏈接)

    JavaScript link方法入門實例(給字符串加上超鏈接)

    這篇文章主要介紹了JavaScript link方法入門實例,link方法用于給字符串加上超鏈接,需要的朋友可以參考下
    2014-10-10
  • 詳解JavaScript中的事件流和事件處理程序

    詳解JavaScript中的事件流和事件處理程序

    事件流指的是從頁面中接收事件的順序,而事件處理程序則是處理事件的響應,接下來我們就來詳解JavaScript中的事件流和事件處理程序.
    2016-05-05
  • this.clientWidth和this.offsetWidth兩個有什么不同

    this.clientWidth和this.offsetWidth兩個有什么不同

    this.clientWidth和this.offsetWidth兩個有什么不同...
    2006-10-10

最新評論