以下两种方法有什么区别吗?#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
})
以下两种方法有什么区别吗?#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
})
在大多数情况下,它们似乎是等价的,尽管“this”似乎更容易输入
根据http://api.jquery.com/event.currentTarget/
event.currentTarget
此属性通常等于
this
函数的 。如果您使用jQuery.proxy或其他形式的范围操作,
this
将等于您提供的任何上下文,而不是event.currentTarget
在这种情况下,他们会做同样的事情。但是 e.currentTarget 将始终引用单击的内容,而 $(this) 将获取您应用处理程序的选择器。