How do I manually trigger a delegated event with jQuery?

后端 未结 5 509
清酒与你
清酒与你 2020-12-08 04:27

Is there a way with jQuery to manually trigger an delegated event handler?

Take following example code:

5条回答
  •  离开以前
    2020-12-08 05:18

    I know, this question is ancient but as I was stumbling over it while looking for an answer to another problem I thought I might as well share my slightly simpler approach here.

    The idea is to simply create the event on the desired target element directly: $(target_selector).trigger(event_type) or - even shorter for standard events like "click" - do $(target_selector).click(), see my little fiddle below:

    $(function(){
     $('.container').on('click','button',function(){
      console.log('delegated click on '+$(this).text());
      return false;
     });
     $('#other').click(e=>$('.container button').trigger('click'));
     $('#clickone').click(e=>$('.container .one').click());
    });
    
    
    and another chance to click here on .

    Trigger clicks on both buttons here!

    Trigger a click on button "one" only.

提交回复
热议问题