简体   繁体   中英

What is the behavior of addEventListener when you try and add a non-existent event?

For example:

someElement.addEventListener("blahblah", alert("hello!"));

when typed into both the Chrome and Firefox development consoles seems to fire the alert("hello!") call once and then return ' undefined '.

If I embed that same call into the page, then nothing seems to happen - no error is fired, no interesting value is returned, etc.

It is just a custom event, which can be also be fired .

However, the problem you got in your test is that you need to pass a function as a handler, not undefined which the invoked alert() returns. Try these:

el.addEventListener("foo", alert.bind(window, "hello!"));

or

el.addEventListener("bar", function(e){ alert("hello "+e.name); });

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