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 事件文档有一个 关于事件顺序的建议.根据它,在复选框的情况下,正确的顺序将是 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:
默认动作应该在事件分发完成后执行,但在特殊情况下也可以在事件分发之前立即执行.
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 两个词!没什么可做的,他们是谨慎的人.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 :)
这篇关于复选框的单击和更改事件顺序是否有标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:复选框的单击和更改事件顺序是否有标准?


- Fetch API 如何获取响应体? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 400或500级别的HTTP响应 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06