JavaScript event.currentTarget vs this
Is there a difference between event.cur开发者_StackOverflow中文版rentTarget
and this
? What about performance?
The currentTarget
event attribute returns the element whose event listeners triggered the event. This is only particularly useful during capturing and bubbling.
You can also use this
keyword, but when you use the Microsoft event registration model the this
keyword doesn’t refer to the HTML element.
Please see following link for more information: http://www.quirksmode.org/js/events_order.html
Problems of the Microsoft model
But when you use the Microsoft event registration model the this keyword doesn’t refer to the HTML element. Combined with the lack of a currentTarget–like property in the Microsoft model, this means that if you do
element1.attachEvent('onclick',doSomething)
element2.attachEvent('onclick',doSomething)
you cannot know which HTML element currently handles the event. This is the most serious problem with the Microsoft event registration model and for me it’s reason enough never to use it, not even in IE/Win only applications.
Note:: it may be,now resolved it
jQuery documentation clearly says, that event.currentTarget
is equal to this
in most cases (unless you changing scope manually with jQuery.proxy
or similar):
This property will typically be equal to the this of the function.
If you are using
jQuery.proxy
or another form of scope manipulation,this
will be equal to whatever context you have provided, notevent.currentTarget
https://api.jquery.com/event.currentTarget/
精彩评论