2012-09-28 99 views
16

以下兩種方法有什麼區別嗎?比#2快#1?

#1 
$('#selector').on('click',function(){ 
    $(this)... 
    // do stuff with clicked element 
}) 

and 

#2 
$('#selector').on('click',function(e){ 
    $(e.currentTarget).... 
    // do stuff with clicked element 
}) 
+1

閱讀該文檔(http://api.jquery.com/ event.currentTarget /)應該給你答案。 – j08691

+0

這個錯誤報告是相關的:http://bugs.jquery.com/ticket/11756。因爲他們認爲這是98%正在做的事情,所以正式使用'this'。 – Medorator

回答

15

好像他們是在大多數情況下等同,但「這」似乎更容易根據http://api.jquery.com/event.currentTarget/

event.currentTarget

鍵入

這個屬性通常等於this的功能。

如果您正在使用jQuery.proxy或範圍操作的另一種形式, this將等於您所提供的任何情況下,不 event.currentTarget

+1

謝謝你的回答 - 我想改變一個事件處理程序來使用ECMA5 .bind()調用,但不知道如何獲得原始的.this上下文和新的.this上下文。 –

相關問題