admin管理员组文章数量:1290925
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>
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>
Share
Improve this question
edited Jun 29, 2020 at 7:09
VLAZ
29.1k9 gold badges62 silver badges84 bronze badges
asked Nov 8, 2014 at 14:03
H23120H23120
3871 gold badge4 silver badges13 bronze badges
1 Answer
Reset to default 8The 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 ment:
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./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).
本文标签: javascriptElement event listener callback not working with custom eventStack Overflow
版权声明:本文标题:javascript - Element event listener callback not working with custom event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741518227a2383024.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论