Multiple JS event handlers on single element

后端 未结 3 613
刺人心
刺人心 2020-12-02 10:59

I am working with an existing web app, in the app there are a variety of submit buttons on different forms, some using regular http post, some defining an onClick function,

3条回答
  •  鱼传尺愫
    2020-12-02 11:13

    jQuery makes this easy.

    $(document).on('click', '.someclass', function() {
      doStuff();
    });
    
    $(document).on('click', '.someclass', function() {
      doMoreStuff();
    });
    

    Handlers then both will fire on click. jQuery keeps a queue of handers for you. And handles document clicks that match a selector of your choice so that they can be triggered no matter when your buttons are created.

提交回复
热议问题