在事件处理程序中使用它 [英] Using this inside an event handler
问题描述
在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.
使用一个参数调用
回调
,其值为Event对象E
,回调此值设置为E
的currentTarget
。
Invoke
callback
with one argument, the value of which is the Event objectE
, with the callback this value set toE
'scurrentTarget
.
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 anEventListener.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 theevent
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 theevent
'scurrentTarget
attribute value.
这篇关于在事件处理程序中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!