2011-10-12 31 views
5

有没有办法允许其他绑定事件到同一个对象(如文本框)首先触发/触发?允许其他绑定元素事件先触发吗?

说2个事件绑定到同一个文本框。这两个keyup事件。在我的例子中,有一个插件绑定自己的事件,但是代码的写法是先绑定我的事件。我不想让我的第一枪开火。

$("#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. 
} 

我需要使用键控,已经有按键事件。

回答

1

你应该真的重写你的代码只有一个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++; 
} 
} 
相关问题