2012-09-28 65 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上下文。 –

相关问题