简体   繁体   English

JavaScript event.currentTarget 与 this

[英]JavaScript event.currentTarget vs this

Is there a difference between event.currentTarget and this ? event.currentTargetthis之间有区别吗? What about performance?性能呢?

The currentTarget event attribute returns the element whose event listeners triggered the event. currentTarget事件属性返回其事件侦听器触发了该事件的元素。 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.您也可以使用this关键字,但是当您使用 Microsoft 事件注册模型时, this关键字不引用 HTML 元素。

Please see following link for more information: http://www.quirksmode.org/js/events_order.html请参阅以下链接了解更多信息: 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.但是当您使用 Microsoft 事件注册模型时, this 关键字并不指代 HTML 元素。 Combined with the lack of a currentTarget–like property in the Microsoft model, this means that if you do结合 Microsoft 模型中缺少类似 currentTarget 的属性,这意味着如果您这样做

element1.attachEvent('onclick',doSomething)
element2.attachEvent('onclick',doSomething)

you cannot know which HTML element currently handles the event.您无法知道当前处理该事件的 HTML 元素。 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.这是 Microsoft 事件注册模型最严重的问题,对我来说,这是永远不要使用它的充分理由,即使在 IE/Win 应用程序中也是如此。

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): jQuery 文档明确指出,在大多数情况下event.currentTarget等于this (除非您使用jQuery.proxy或类似工具手动更改范围):

This property will typically be equal to the this of the function.此属性通常等于函数的 this。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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