开发者

Allow other bound element events to trigger first?

Is there a way to allow other bound events to the same object(ex. textbox) to fire/trigger first?

Say 2 events got bound to the same textbox. Both keyup events. In my case there is a plugin binding its own events, b开发者_如何学编程ut the way the code is written, mine get bound first. I do not want mine to fire first.

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

I need to use keyup, there is already key down events.


You should really rewrite your code to only have one keyup binding to that event but if that isn't feasible, you could do it dirty with semaphores and separate your functionality from the binding so it can be called from either 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++; 
 }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜