jQuery prop和attr的區別

來源:互聯網
上載者:User

jQuery prop和attr的區別
兩者對比 jquery方法 原理 適合情境 缺陷prop 解析原生propertyelement.property radio/checkbox select標籤等需要讀boolean和索引的場合 讀不到自訂屬性如<a my='I'/> my屬性讀不到attr 通過Attr API去讀取element.getAttribute(propertyName) 除prop情境外 可能讀不到boolean或一些索引值如checked,selectedIndex  prop方法   例子     在控制台輸入 document.getElementsByTagName('a')[0].href    控制台輸出 "http://www.baidu.com/home/xman/show/liteoff"     href就是標籤a映射的DOM對象HTMLAnchorElement的原生屬性。       jQuery源碼       複製代碼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 );//這裡就是讀原生property            }         } else {            if ( hooks && "get" in hooks && (ret = hooks.get( elem, name )) !== null ) {                return ret;             } else {                return elem[ name ];            }        }    }複製代碼      attr方法   例子     在控制台輸入 document.getElementsByTagName('a')[10].getAttribute('href')   控制台輸出 "http://www.hao123.com"   getAttribute就是Attr類的查詢API。其他還有刪除、修改、增加。      jQuery源碼  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 );//這裡通過Attr API讀取屬性             // Non-existent attributes return null, we normalize to undefined            return ret === null ?                undefined :                ret;        }    }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.