gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 04:27:47 26 4
gpt4 key购买 nike

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

根据我的实验,this 指的是 event.currentTarget 对象。

标准中是否提到了这种行为?

根据“JavaScript 权威指南”一书,this 指的是似乎错误的事件目标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.

但是,当前版本省略了这些绑定(bind)。在其词汇表附录中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 current event target) 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 callback this value is the event's currentTarget attribute value.

关于javascript - 在事件处理程序中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17665489/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com