jQuery addClass() 源碼解讀

來源:互聯網
上載者:User

標籤:

    addClass: function( value ) {        var classes, elem, cur, clazz, j,            i = 0,            len = this.length,            proceed = typeof value === "string" && value;        if ( jQuery.isFunction( value ) ) {            return this.each(function( j ) {                jQuery( this ).addClass( value.call( this, j, this.className ) );            });        }        if ( proceed ) {//如果傳入的值不是字串            // The disjunction here is for better compressibility (see removeClass)            //如果傳入的是假值,"".match()              //core_rnotwhite 用於匹配非空白字元,且有g標識,匹配成功返回數組,匹配不成功返回[]            classes = ( value || "" ).match( core_rnotwhite ) || [];            //rclass = /[\t\r\n\f]/g            for ( ; i < len; i++ ) {                elem = this[ i ];                //如果elem是元素,cur會返回真值," "或者 " a b "                //替換tab符 斷行符號 換行 換頁                 cur = elem.nodeType === 1 && ( elem.className ?                    ( " " + elem.className + " " ).replace( rclass, " " ) :                    " "                );                if ( cur ) {                    j = 0;                    //遍曆,如果原始class中不存在,字串拼接                    while ( (clazz = classes[j++]) ) {                        if ( cur.indexOf( " " + clazz + " " ) < 0 ) {                            cur += clazz + " ";                        }                    }                    elem.className = jQuery.trim( cur );//最後兩邊去空格                }            }        }        return this;    }

removeClass與addClass代碼類似。

 

jQuery addClass() 源碼解讀

聯繫我們

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