JavaScript中的事件处理程序是否按顺序调用? [英] Are event handlers in JavaScript called in order?

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

问题描述

如果我将多个事件处理程序附加到单个DOM元素上的单个事件中,事件处理程序是否按照添加的顺序进行调用?或者我不应该依赖这种行为?

解决方案

这个已更改虽然 DOM 2级事件规范确实状态为/ p>


当事件达到目标时,任何事件监听器都注册在
EventTarget
被触发。虽然所有 EventListeners EventTarget 中的
保证由任何
EventTarget 无规范被设为
订单
,他们将收到与其他
EventListeners EventTarget


目前的 DOM 3级事件规范现在表示


实现必须确定当前目标的候选人
事件侦听器

这必须是当前目标以注册顺序注册
的所有事件监听器的列表。 HTML5
定义侦听器的顺序通过事件处理程序
属性注册

[...]



最后,实现必须处理所有候选人事件
处理程序

按顺序 [...]




但是,在 DOM 4草案


If I attach multiple event handlers to a single event on a single DOM element, are the event handlers guaranteed to be called in the order they were added? Or should I not rely on this behavior?

解决方案

This has been changed with DOM3! While the DOM level 2 events specification did state

When the event reaches the target, any event listeners registered on the EventTarget are triggered. Although all EventListeners on the EventTarget are guaranteed to be triggered by any event which is received by that EventTarget, no specification is made as to the order in which they will receive the event with regards to the other EventListeners on the EventTarget.

The current DOM level 3 events specification does now state

The implementation MUST determine the current target's candidate event listeners. This MUST be the list of all event listeners that have been registered on the current target in their order of registration. HTML5 defines the ordering of listeners registered through event handler attributes. […]

Finally, the implementation MUST process all candidate event handlers in order […]

However, I can't find a reference to this behaviour in the DOM 4 draft any more.

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

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