复选框的单击和更改事件顺序是否有标准? [英] Is there a standard for the event order of click and change on a checkbox?

查看:25
本文介绍了复选框的单击和更改事件顺序是否有标准?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到 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 事件显然是一部分复选框的默认操作,请参见 示例 5fiddle,它们在 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 的 SHOULDMAY 两个词!没什么可做的,他们是谨慎的人.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 :)

这篇关于复选框的单击和更改事件顺序是否有标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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