微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 在事件处理程序中使用它

我试图在DOM级别3事件规范中的事件处理函数内找到此关键字的含义.

根据我的实验,这引用了event.currentTarget对象.

这种行为是否在标准的某处提到过?

根据“JavaScript The Definitive Guide”一书,这指的是似乎错误的事件目标. event.currentTarget似乎更合乎逻辑,因为事件处理程序被调用为HTML元素对象的方法.

有人可以澄清一下吗?

In case of bubbling I see “this” changes and means the
event.currentTarget.

解决方法

实际上,在这种情况下,权威指南是错误的.

我在HTML5 event handler processing algorithm中找到了一个参考:

Invoke callback with one argument,the value of which is the Event object E,with the callback this value set to E‘s currentTarget.

DOM 3级事件规范在以前的版本中没有说太多 – 它本来是语言无关的. Appendix F: ECMAScript Language Binding刚才说明了

EventListener function:
This function has no return value. The parameter shall be an object that implements the Event interface.

但是,当前版本省略了这些绑定.在其词汇表附录event listeners中描述:

event handler,event listener: An object that implements the EventListener interface and provides an EventListener.handleEvent() callback method. Event handlers are language-specific. Event handlers are invoked in the context of a particular object (the 07003) and are provided with the event object itself.

此外,即将推出的DOM Level 4草案,其goals包含将DOM与EcmaScript的需求对齐,确实明确说明了in the Dispatching Events section

If listener‘s callback is a Function object,its 07006 is the event‘s 07007 attribute value.

原文地址:https://www.jb51.cc/js/158452.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐