使用jquery 1.7 及以後的版本 attr 問題

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   使用   ar   sp   div   

跟進jquery的代碼進行檢查,發現問題出在下面的代碼中:

if ( notxml ) {            name = name.toLowerCase();            hooks = jQuery.attrHooks[ name ] || ( rboolean.test( name ) ? boolHook : nodeHook );        }

此處將屬性名稱變成了全小寫,導致dom對象內建的屬性無法取到值。

代碼所在位置第2298行,

attr函數完整代碼:

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;        }

測試環境 IE,FF

使用jquery 1.7 及以後的版本 attr 問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.