开发者

JavaScript event.currentTarget vs this

开发者 https://www.devze.com 2023-02-13 08:26 出处:网络
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 e

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, not event.currentTarget

https://api.jquery.com/event.currentTarget/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号