詳談jQuery中的this和$(this),詳談jquery中this

來源:互聯網
上載者:User

詳談jQuery中的this和$(this),詳談jquery中this

網上有很多關於jQuery的this和$(this)的介紹,大多數只是理清了this和$(this)的指向,其實它是有應用場所的,不能一概而論在jQuery調用成員函數時,this就是指向dom對象。

$(this)指向jQuery對象是無可厚非的,但this就是指向dom對象,這個是因為jQuery做了特殊的處理。 

在建立dom的jQuery對象時,jQuery不僅僅為dom建立一個jQuery對象,而且還將dom儲存在所建立對象的數組中。

複製代碼 代碼如下:
elem = document.getElementById(match[2]); 
if (elem && elem.parentNode) { 
  this.length = 1; 
  this[0] = elem; 

 
this.context = document; 
this.selector = selector; 
return this; 

 this[0] = elem這條語句就是實現對象數組。所以javascript是很有意思的語言,使用this訪問時,可以訪問它所指向的對象的成員函數,而其實this又是一個對象數組。其存放的是dom對象。

先看看 $("p").each() -- 迴圈

複製代碼 代碼如下:
each: function( callback, args ) { 
        return jQuery.each( this, callback, args ); 
    } 

 看了each函數的調用大家應該明白,jQuery.each( this, callback, args );調用的是對象數組,而對象的數組儲存的是dom對象,因此在callback函數中的this自然是dom對象了

再看看$("p").hide() -- 成員函數

複製代碼 代碼如下:
hide: function() { 
        return showHide( this ); 
    }, 
 function showHide( elements, show ) {var elem, display, 
        values = [], 
        index = 0, 
        length = elements.length; 
    for ( ; index < length; index++ ) { 
        elem = elements[ index ]; 
        if ( !elem.style ) { 
            continue; 
        } 
        values[ index ] = jQuery._data( elem, "olddisplay" ); 
        if ( show ) { 
            // Reset the inline display of this element to learn if it is 
            // being hidden by cascaded rules or not 
            if ( !values[ index ] && elem.style.display === "none" ) { 
                elem.style.display = ""; 
            } 
            // Set elements which have been overridden with display: none 
            // in a stylesheet to whatever the default browser style is 
            // for such an element 
            if ( elem.style.display === "" && isHidden( elem ) ) { 
                values[ index ] = jQuery._data( elem, "olddisplay", css_defaultDisplay(elem.nodeName) ); 
            } 
        } else { 
            display = curCSS( elem, "display" ); 
            if ( !values[ index ] && display !== "none" ) { 
                jQuery._data( elem, "olddisplay", display ); 
            } 
        } 
    } 
    // Set the display of most of the elements in a second loop 
    // to avoid the constant reflow 
    for ( index = 0; index < length; index++ ) { 
        elem = elements[ index ]; 
        if ( !elem.style ) { 
            continue; 
        } 
        if ( !show || elem.style.display === "none" || elem.style.display === "" ) { 
            elem.style.display = show ? values[ index ] || "" : "none"; 
        } 
    } 
    return elements; 

 從上面的代碼可以看出hide行數其實調用的是showHide,而傳入的第一個參數this,並不是dom對象,而是jQuery對象數組,因此showHide函數通過迴圈此對象數組擷取每一個dom對象。

最後看看$("p").bind() -- 事件

複製代碼 代碼如下:
bind: function( types, data, fn ) { 
        return this.on( types, null, data, fn ); 
    }, 

複製代碼 代碼如下:
on: function( types, selector, data, fn, /*INTERNAL*/ one ) { 
        // 此部分代碼省略 
        return this.each( function() { 
            jQuery.event.add( this, types, fn, data, selector ); 
        }); 
    }, 

bind函數調用的是 on函數,而on函數又是通過 each函數實現了jQuery.event.add。因此 jQuery.event.add( this中的this也就是dom對象了。所以事件中的this也就是dom對象了。

以上就是個人對於jQuery中this與$(this)的理解了,如有什麼紕漏,請聯絡我或者給我留言

聯繫我們

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