<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 8,  comments - 6,  trackbacks - 0

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

    之前看網上對比兩者的文章,更是列出一個表來區分什么標簽下使用prop,什么標簽下使用attr,原諒我是懶惰的人,最害怕要背的東西,所以只有自己想想辦法了。

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

    attr方法代碼(jQuery版本1.8.3)

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

    我們接著看:

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

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

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

    我們再接著來:

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

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

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

    提一下,在遇到要獲取或設置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屬性節點,訪問DOM是最耗時的。這種情況適用于多選項全選和反選的情況。

    posted on 2015-01-21 14:33 ♂游泳的魚 閱讀(1843) 評論(1)  編輯  收藏

    FeedBack:
    # re: [JS]jQuery中attr和prop方法的區別[未登錄]
    2015-01-27 15:03 | 星情
    分析得不錯  回復  更多評論
      

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    <2015年1月>
    28293031123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    留言簿(1)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    •  

    最新評論

    閱讀排行榜

    主站蜘蛛池模板: mm1313亚洲国产精品无码试看| 亚洲视频一区在线播放| 亚洲日韩AV一区二区三区中文| 亚洲国产精品免费在线观看| 亚洲国产第一页www| 日本一区二区免费看| 久久精品国产精品亚洲色婷婷| 中国一级全黄的免费观看| 亚洲成A∨人片在线观看不卡| 四虎国产精品免费永久在线| 国产V亚洲V天堂无码| 最近中文字幕免费mv在线视频| 亚洲精品国产成人| 久久www免费人成看片| 亚洲一级特黄特黄的大片| 精品久久免费视频| 看成年女人免费午夜视频| AV在线亚洲男人的天堂| 免费无码黄网站在线看| 亚洲一区二区三区不卡在线播放| 国产人在线成免费视频| 亚洲av无码专区国产不乱码 | 日韩视频在线精品视频免费观看| 亚洲三级在线播放| 成人免费无码精品国产电影| 四虎精品成人免费视频| 日韩亚洲欧洲在线com91tv| 9420免费高清在线视频| 亚洲国产精品无码久久98| 亚洲一本大道无码av天堂| 午夜免费福利小电影| 国产日本亚洲一区二区三区| 免费一级成人毛片| 无码国产精品一区二区免费3p | 亚洲视频免费观看| 亚洲精华液一二三产区| 亚洲精品亚洲人成人网| 青苹果乐园免费高清在线| 一个人免费观看日本www视频| 97se亚洲综合在线| 免费a在线观看播放|