在事件处理程序中使用它 [英] Using this inside an event handler

查看:74
本文介绍了在事件处理程序中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was trying to find the meaning of this keyword inside event handler function in the DOM level 3 event spec.

根据我的实验这个是指 event.currentTarget 对象。

As per my experiment this refers to the event.currentTarget object.

这个行为是在标准中的某个地方提到的吗?

Is this behavior mentioned somewhere in the standard?

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

As per "JavaScript The Definitive Guide" book this refers to the event target which seems to wrong. event.currentTarget seems more logical as event handlers are invoked as the method of the HTML element object.

有人可以请澄清?


如果冒泡,我看到this更改,意味着
event.currentTarget。

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


推荐答案

确实,在这种情况下,最终指南是错误的。

Indeed, the Definitive Guide is wrong in that case.

我在中找到了一个引用HTML5事件处理程序处理算法


使用一个参数调用回调 ,其值为Event对象 E ,回调此值设置为 E currentTarget

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级事件规范没有'在以前的版本中就会说得很多 - 这意味着是语言agno STIC。 附录F:ECMAScript语言绑定 刚刚说明

The DOM level 3 event specification didn't say much about it in previous versions - it was meant to be language agnostic. The Appendix F: ECMAScript Language Binding just stated


EventListener函数
此函数没有返回值。参数应该是一个实现 Event 接口的对象。

当前版本省略了这些绑定。并在其术语表附录中事件侦听器被描述:

However, current versions omitted these bindings. And in its Glossary appendix event listeners are described:


事件处理程序事件侦听器 :实现 EventListener 接口的对象,并提供一个 EventListener.handleEvent()回调方法。事件处理程序是语言特定的。事件处理程序在特定对象的上下文中被调用 当前事件目标 ),并附带事件对象本身。

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 current event target) and are provided with the event object itself.

另外,即将到来的DOM Level 4草案,其目标包含将DOM与EcmaScript的需求对齐,明确指出调度事件部分中的listener-invokerel =nofollow>

Also, the upcoming DOM Level 4 draft, whose goals contain aligning the DOM with the needs of EcmaScript, does explicitly state in the Dispatching Events section:


如果监听器回调是一个Function对象,其回调此值事件 currentTarget 属性值。

If listener's callback is a Function object, its callback this value is the event's currentTarget attribute value.

这篇关于在事件处理程序中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆