admin管理员组文章数量:1332361
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?
- 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
1 Answer
Reset to default 9JQuery 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.
本文标签: javascriptListen to jQuery event without jQueryStack Overflow
版权声明:本文标题:javascript - Listen to jQuery event without jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742318682a2452335.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论