javascript - Listen to jQuery event without jQuery - Stack Overflow

Is there a way to set up an event listener, so it captures an event fired by jQuery? I want to use only

Is there a way to set up an event listener, so it captures an event fired by jQuery? I want to use only plain (vanilla) JavaScript to listen to the event.

I have set up the following example:

(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));

Three events which are received and logged are:

  • sent_vanilla_rec_vanilla
  • sent_vanilla_rec_jquery
  • sent_jquery_rec_jquery

When the event is sent via $(document).trigger, the event listener set up with document.addEventListener does not kick in. What's the reason for that, and how can I make it work?

Is there a way to set up an event listener, so it captures an event fired by jQuery? I want to use only plain (vanilla) JavaScript to listen to the event.

I have set up the following example:

(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));

Three events which are received and logged are:

  • sent_vanilla_rec_vanilla
  • sent_vanilla_rec_jquery
  • sent_jquery_rec_jquery

When the event is sent via $(document).trigger, the event listener set up with document.addEventListener does not kick in. What's the reason for that, and how can I make it work?

Share Improve this question edited Jan 11, 2022 at 7:37 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 10, 2015 at 13:04 Pavel S.Pavel S. 12.4k19 gold badges77 silver badges113 bronze badges 2
  • Does it make a difference if you addEventListener after the triggers are registered? – Chad McGrath Commented Sep 10, 2015 at 13:18
  • 2 May this be related to this? stackoverflow./questions/18823872/… – Danilo Commented Sep 10, 2015 at 14:04
Add a ment  | 

1 Answer 1

Reset to default 9

JQuery apparently uses a custom event system, so you can't listen to jQuery events with a regular event listener. The aforementioned ticket mentions IE <9 as the culprit here, maybe jQuery 3 or 4 could make the switch.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742318682a4421359.html

相关推荐

  • javascript - Listen to jQuery event without jQuery - Stack Overflow

    Is there a way to set up an event listener, so it captures an event fired by jQuery? I want to use only

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信