Iterate getEventListeners() return object

后端 未结 2 1540
耶瑟儿~
耶瑟儿~ 2020-12-18 09:47

I am searching for a way to iterate the object getEventListeners(obj) returns. This way, I wouldn\'t need specific code to iterate event listener types, or to c

相关标签:
2条回答
  • 2020-12-18 10:27

    getEventListeners() will return simple JS object, you can iterate objects like this:

    var listeners = window.getEventListeners(document.body);
    Object.keys(listeners).forEach(event => {
        console.log(event, listeners[event]);
    });
    

    But looks like that getEventListeners method is available only in chrome, not sure what is your use case, but you might want to use different method for getting event listeners.

    0 讨论(0)
  • 2020-12-18 10:37

    getEventListeners(obj) is only a Google Chrome specific Command Line Tool feature. This means that you can only use this feature inside Chrome Dev Tools when manually typing into the console. You cannot use this method in your actual JavaScript source code.

    If you want to achieve what you described, AFAIK you have to keep track of your listeners manually. Check this answer for further instructions.

    0 讨论(0)
提交回复
热议问题