How do I manually trigger a delegated event with jQuery?

后端 未结 5 513
清酒与你
清酒与你 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 04:58

    We can pass an additional event configuration to jQuery's $.Event() as the second argument. More information here.

    $('#click-me').on('click', function(evt){
      $(document).trigger($.Event('custom-click', {target: evt.currentTarget}));
    });
    
    $(document).on('custom-click', '#click-me', function(evt){
      alert(`${evt.type} was triggered on button with ${evt.target.id} id.`);
    })
    
    
    
    

    Good Luck...

提交回复
热议问题