JavaScript event.currentTarget 与 this
event.currentTarget
和 this
之间有区别吗?性能怎么样?
Is there a difference between event.currentTarget
and this
? What about performance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
currentTarget
事件属性返回其事件侦听器触发该事件的元素。这仅在捕获和冒泡期间特别有用。您还可以使用
this
关键字,但当您使用 Microsoft 事件注册模型时,this
关键字不引用 HTML 元素。请参阅以下链接了解更多信息:http://www.quirksmode.org/js/events_order。 html
Microsoft 模型的问题
但是,当您使用 Microsoft 事件注册模型时,this 关键字并不引用 HTML 元素。再加上 Microsoft 模型中缺少类似 currentTarget 的属性,这意味着如果您这样做,
您将无法知道当前哪个 HTML 元素正在处理该事件。这是 Microsoft 事件注册模型最严重的问题,对我来说,这就是我永远不使用它的理由,即使在仅 IE/Win 的应用程序中也是如此。
注::可能是这样,现在解决了
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 thethis
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
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 文档清楚地表明,在大多数情况下
event.currentTarget
等于this
(除非您使用jQuery.proxy
或类似工具手动更改范围) :https://api.jquery.com/event.currentTarget/
jQuery documentation clearly says, that
event.currentTarget
is equal tothis
in most cases (unless you changing scope manually withjQuery.proxy
or similar):https://api.jquery.com/event.currentTarget/