gpt4 book ai didi

javascript - 让其他绑定(bind)的元素事件先触发?

转载 作者:搜寻专家 更新时间:2023-11-01 04:38:48 27 4
gpt4 key购买 nike

有没有办法让同一对象(例如文本框)的其他绑定(bind)事件先触发/触发?

假设 2 个事件绑定(bind)到同一个文本框。两个 keyup 事件。在我的例子中,有一个插件绑定(bind)了它自己的事件,但是代码的编写方式,我的首先被绑定(bind)。我不想让我的先开火。

$("#firstname").keyup(function() {
// ...is there anyway to allow the other keyup event to fire first, from here?
// do my work here...
}

$("#firstname").keyup(function() {
// the plugin work.
}

我需要使用keyup,已经有key down事件了。

最佳答案

你真的应该重写你的代码,让只有一个 keyup 绑定(bind)到那个事件,但如果那不可行,你可以用信号量来做它,并将你的功能与绑定(bind)分开,这样它就可以从任一绑定(bind)调用.. .

var semaphore = 0; // on init

$("#firstname").keyup(function () { // this one should run first
semaphore++;

if (semaphore === 0) {
first_action();
}
}

$("#firstname").keyup(function () { // this one should run second
if (semaphore > 1) { // you know the first event fired
second_action();
}
else if (semaphore < 1) {
first_action();
second_action();
semaphore++;
}
}

关于javascript - 让其他绑定(bind)的元素事件先触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7744838/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com