问题描述
我注意到 Chrome 和 Firefox 中点击"和更改"事件的顺序不同.
I've noticed that the order of 'click' and 'change' events are different in Chrome and Firefox.
例如查看此 JSFiddle:http://jsfiddle.net/5jz2g/3/
See this JSFiddle for example: http://jsfiddle.net/5jz2g/3/
JavaScript:
JavaScript:
var el = $('foo');
var fn = function(e) {
console.log(e.type);
}
el.addEvent('change', fn);
el.addEvent('click', fn);
在 Chrome 中,此日志:
In Chrome this logs:
change
click
在 Firefox 中记录:
And in Firefox this logs:
click
change
事件的顺序有标准吗?哪个应该先开火?MDN 似乎没有提到这一点,我在 W3C 文档中也找不到相关内容.
Is there a standard for the order of events? Which should fire first? The MDN doesn't seem to mention this and I couldn't find a thing about this in the W3C documents.
推荐答案
DOM3 Events 文档有一个 推荐 关于事件顺序.根据它,在复选框的情况下,正确的顺序将是 click
然后 change
而不是反之亦然,因为 change
事件显然是一部分复选框的默认操作,请参阅示例 5 和 fiddle,在 FF 中按预期工作,但在 Chrome 中不工作.无论如何,这是合乎逻辑的.但!让我们仔细阅读默认操作部分:
DOM3 Events document has a recommendation about events order. According to it, in case of checkbox, the correct order will be click
then change
and not vice versa, because change
event obviously is a part of default actions for checkbox, see Example 5 and fiddle, which works as expected in FF but not in Chrome. That's logical, anyway. But! Let's read default actions section carefully:
默认操作应该在事件派发完成后执行,但在特殊情况下也可以在事件派发前立即执行.
你看到了吗?W3C 在一句话中使用了 RFC 的词SHOULD 和 MAY!没什么可做的,他们是谨慎的家伙.IMO,这就是为什么我们拥有我们所拥有的 :)
Did you see that? W3C uses RFC's words SHOULD and MAY in one sentence! Nothing to be done, they're cautious guys. IMO, that's why we have what we have :)
这篇关于单击和更改复选框的事件顺序是否有标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!