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

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

问题描述

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

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 已随 DOM3 更改!而 DOM 级别 2 事件规范 做了状态

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

当事件到达目标时,任何在事件监听器上注册的事件监听器EventTarget被触发.尽管所有 EventListenersEventTarget 保证由任何事件触发由 EventTarget 接收,没有指定作为 他们接收事件的顺序EventTarget 上的 EventListeners.

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.

当前的 DOM 级别 3 事件规范 现在声明

实现必须确定当前目标的候选事件监听器.这必须是已注册的所有事件侦听器的列表在当前目标按照他们的注册顺序.HTML5定义侦听器的顺序 注册通过事件处理程序属性.[…]

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 […]

但是,我在 中找不到对这种行为的引用DOM 4 草稿.

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

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