對於this和$(this)的個人理解

來源:互聯網
上載者:User

複製代碼 代碼如下:
jQuery.prototype.test=function(){
this.css("color","#99");//這裡的this是jquery對象,而不是dom對象
alert(this[0]);//這裡的this[0]指的是dom節點對象
}

複製代碼 代碼如下:
$("body").click(function(){
$(this).test();
$(this).test().html(this.nodeName).hide(10000);
})

在頁面上使用click方法的時候相當於new了一個Jquery對象然後調用他的click方法,方法裡面的參數是一個javascript的function函數,裡面的this指的是javascript對象,this是javascript自身的 文法關鍵字,它指向一個javascript對象,所以可以使用所指向的目標javascript對象所擁有的方法

而jQuery.prototype.test相當於在就query對象中建立一個test的方法,所以裡面的this理所應當的就是就jquery對象

通過this[0]可以把jquery對象轉化成dom節點對象
因為this永遠指向調用該方法(函數)的那個對象(call,apply方法除外)

相關文章

聯繫我們

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