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.

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...