简体   繁体   中英

firing multiple jquery event listeners consecutively

Say I'm doing something like this:

$('#demo').click(func1);
$('#demo').click(func2);

In Firefox that makes func1() run after func2() but not on IE.

Any ideas?

Binding same event to same element multiple times is not necessary, though it has no problem with jQuery.

Just simply write

$('#demo').click(function() {
  // serial execution of functions
  func1();
  func2();
});

If you need to bind click for multiple elements then procedure will be

$('#demo1, #demo2').click(function() {
   // do your stuff
});

you can use .on() for event bind like:

$('#demo').on('click', function() {
  func1();
  func2();
});

Instead of binding event twice call both function in click event. You can do it without binding event twice like this.

$('#demo').click(function (){

func1(); 

func2();

});

If you want to bind it multiple times then you can do like this, Binding event for multiple times without good reason should be avoided though. Demo on JsFiddle

$('#demo').bind('click', function(){
   alert("fun1");})
    .bind('click', function(){
  alert("fun2");
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM