在没有jQuery的情况下收听jQuery事件

Pav*_* S. 7 javascript jquery javascript-events addeventlistener

有没有办法设置事件监听器,所以它捕获jQuery触发的事件?我想只使用普通(vanilla)JavaScript来收听事件.

我已经设置了以下示例:

(function($) {

    $(document).on('sent_jquery_rec_jquery', function () {
        console.log('sent_jquery_rec_jquery');
    });

    $(document).on('sent_vanilla_rec_jquery', function () {
        console.log('sent_vanilla_rec_jquery');
    });

    document.addEventListener('sent_vanilla_rec_vanilla', function () {
        console.log('sent_vanilla_rec_vanilla');
    });

    document.addEventListener('sent_jquery_rec_vanilla', function () {
        console.log('sent_jquery_rec_vanilla');
    });

    document.dispatchEvent(new Event("sent_vanilla_rec_vanilla"));
    document.dispatchEvent(new Event("sent_vanilla_rec_jquery"));

    $(document).trigger('sent_jquery_rec_jquery');
    $(document).trigger('sent_jquery_rec_vanilla');

}(jQuery));
Run Code Online (Sandbox Code Playgroud)

接收和记录的三个事件是:

  • sent_vanilla_rec_vanilla
  • sent_vanilla_rec_jquery
  • sent_jquery_rec_jquery

当事件通过发送时$(document).trigger,设置的事件监听document.addEventListener器没有启动.这是什么原因,我怎样才能使它工作?

Ind*_*ing 5

JQuery 显然使用了自定义事件系统,因此您不能使用常规事件侦听器来侦听jQuery事件。前面提到的票证提到IE <9是罪魁祸首,也许jQuery 3或4可以进行切换。