复选框单击事件在firefox中更改事件之前触发,与chrome不同 [英] checkbox click event fires before change event in firefox unlike in chrome

查看:109
本文介绍了复选框单击事件在firefox中更改事件之前触发,与chrome不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本中有一个复选框的两个事件,一个点击,另一个点击更改。
当我点击它时它首先触发更改事件然后点击chrome中的事件但是在Firefox中它是反向的。有没有办法在Firefox中更改它的顺序?

I have two events for a check-box in my script one on click and another on change. when i click on it it fires change event first then click event in chrome but in Firefox it's reverse.is there any way to change the order of it in Firefox?

推荐答案

我不知道为什么触发事件的顺序在浏览器之间是不一致的,但我不明白为什么你不能将所有东西都移到一个更改处理程序。如果你必须使用单独的处理程序,你可以添加一个超时,click事件仍然会在Firefox中首先触发,但是在更改处理程序触发之后它的代码才会被执行。

I don't know why the order in which the events are fired is inconsistent between browsers, but I don't see why you can't move everything in to a single change handler. If you must use separate handlers, you could add a timeout, the click event would still fire first in Firefox, but its code wouldn't be executed until after the change handler fires.

var myBoolean = false;

$('#example').on({
    'click': function() {
        setTimeout(function() {
            console.log('clicked'); 
            console.log(myBoolean);
        }, 1);
    },
    'change': function() {
        console.log('changed');
        myBoolean = true;
    }
});

这篇关于复选框单击事件在firefox中更改事件之前触发,与chrome不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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