jQuery trigger not firing with bind() or on() for custom events

前端 未结 2 1387
北海茫月
北海茫月 2021-02-19 08:15

Can anyone tell me why this code would not be working?

$(\'body\').on(\'test\', function() {
  alert(\'test\');
});

$(\'body\').trigger(\'test\');
2条回答
  •  礼貌的吻别
    2021-02-19 08:44

    Try delegation:

    $(document).on('test', 'body', function() {
      alert('test');
    });
    
    $('body').trigger('test');
    

    This works like live() used to. http://api.jquery.com/live/

提交回复
热议问题