简体   繁体   中英

Element event listener callback not working with custom event

Element event listener callback not working with custom event, in the following code, document and window event listeners will be triggered, but not element's (tested on IE11, FF33, and Chrome38, results are the same.) Any idea? Did I misuse custom event?

 var el = document.getElementById('some-elem'); el.addEventListener('custom', function (e) { console.log("Element got event: " + e.type); }, false, true); document.addEventListener('custom', function (e) { console.log("document got event: " + e.type); }, false); window.addEventListener('custom', function (e) { console.log("window got event: " + e.type); }, false); var evt = document.createEvent('CustomEvent'); evt.initCustomEvent('custom', true, false); document.dispatchEvent(evt);
 <div id="some-elem"></div>

The problem is that the event never goes through the element, you're firing the event on document . Instead, fire it on the element:

 var el = document.getElementById('some-elem'); el.addEventListener('custom', function (e) { console.log("Element got event: " + e.type); }, false, true); document.addEventListener('custom', function (e) { console.log("document got event: " + e.type); }, false); window.addEventListener('custom', function (e) { console.log("window got event: " + e.type); }, false); var evt = document.createEvent('CustomEvent'); evt.initCustomEvent('custom', true, false); el.dispatchEvent(evt); // <=== Change is here
 <div id="some-elem"></div>


Re your comment:

the object dispatching the event is different from the listening object in my case, does it mean custom event can only be listened on the same object (except window and document)?

The event has to travel through the element, just like a click has to travel through the element for it to be seen by a click handler on the element. So for instance, if you had a div with the event handler on it, and there was an img inside the div , and you dispatched the event on the img , the handler on the div would trigger because the event bubbles to it (because you made the event bubble-able):

 var el = document.getElementById('some-elem'); el.addEventListener('custom', function (e) { snippet.log("Element got event: " + e.type); }, false, true); document.addEventListener('custom', function (e) { snippet.log("document got event: " + e.type); }, false); window.addEventListener('custom', function (e) { snippet.log("window got event: " + e.type); }, false); var evt = document.createEvent('CustomEvent'); evt.initCustomEvent('custom', true, false); document.getElementById("some-span").dispatchEvent(evt); // <=== Change is here
 <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script><!-- snippet.js details: http://meta.stackexchange.com/a/242144/134069 --> <div id="some-elem"> <span id="some-span"></span> </div>

This diagram from the DOM events spec may help with picturing this stuff:

在此处输入图片说明

The element on which you call dispatchEvent is the "target" element (the dark blue td in that diagram).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM