简体   繁体   中英

JavaScript event.currentTarget vs this

Is there a difference between event.currentTarget 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM