有没有办法允许其他绑定事件到同一个对象(例如文本框)首先触发/触发?
假设2个事件绑定到同一个文本框.两个键盘事件.在我的情况下,有一个插件绑定自己的事件,但编写代码的方式,我的第一个绑定.我不希望我先开火.
$("#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.
}
Run Code Online (Sandbox Code Playgroud)
我需要使用keyup,已经有按键事件.
您确实应该重写您的代码,使其只有一个 keyup 绑定到该事件,但如果这不可行,您可以使用信号量进行修改,并将您的功能与绑定分开,以便可以从任一绑定调用它...
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++;
}
}
Run Code Online (Sandbox Code Playgroud)