迭代getEventListeners()返回对象 [英] Iterate getEventListeners() return object

查看:573
本文介绍了迭代getEventListeners()返回对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找迭代对象getEventListeners(obj)返回的方法。
这样,我不需要特定的代码来迭代事件监听器类型,或检查它们是否存在于checked元素上。
我的目标是从元素中删除一些事件侦听器。例如,移除所有鼠标相关的,并保留其他鼠标。

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 check if they exist on the checked element. My goal is to remove some event listeners from an element. For example remove all mouse related ones, and keep the others.

谢谢

推荐答案

getEventListeners()将返回简单的JS对象,你可以迭代这样的对象:

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]);
});

但看起来像 getEventListeners 方法可用仅在chrome中,不确定您的用例是什么,但您可能希望使用不同的方法来获取事件侦听器。

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.

这篇关于迭代getEventListeners()返回对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆