复选框上的点击和更改事件顺序是否有标准? [英] Is there a standard for the event order of click and change on a checkbox?
问题描述
我注意到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事件文档有推荐事件顺序。根据它,如果是复选框,正确的顺序将是点击
然后更改
而不是相反,因为更改
事件显然是复选框默认操作的一部分,请参阅示例5 和小提琴,在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:
应在事件发送完成后执行默认操作,但在特殊情况下也可执行在事件发出之前。
Default actions should be performed after the event dispatch has been completed, but in exceptional cases may also be performed immediately before the event is dispatched.
你看到了吗? W3C在一个句子中使用RFC的单词 SHOULD 和 MAY !没什么可做的,他们是谨慎的家伙。国际海事组织,这就是为什么我们拥有我们拥有的东西:)
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 :)
这篇关于复选框上的点击和更改事件顺序是否有标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!