18

以下两种方法有什么区别吗?#1 比 #2 快吗?

#1

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

#2

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

2 回答 2

16

在大多数情况下,它们似乎是等价的,尽管“this”似乎更容易输入

根据http://api.jquery.com/event.currentTarget/

event.currentTarget

此属性通常等于this函数的 。

如果您使用jQuery.proxy或其他形式的范围操作, this将等于您提供的任何上下文,而不是 event.currentTarget

于 2012-09-28T01:36:36.790 回答
1

在这种情况下,他们会做同样的事情。但是 e.currentTarget 将始终引用单击的内容,而 $(this) 将获取您应用处理程序的选择器。

于 2021-10-12T15:02:05.197 回答