首页 > 解决方案 > How to remove all event listeners from certain categories that are mentioned in Chrome Console in Event Listeners

问题描述

How do I remove all event listeners that are connected to the element that I can see in the Chrome Console in Event Listeners?

jQuery can be used if it will make it easier.

For example:

This is tab in Chrome console. Chrome Console

As you can see element is <a> but it as also some listeners from <div>, there is remove button next to each listener.

I would like to replicate something like this Remove button that deletes all listeners from mouseleave and mouseenter category.

I have tried to use jQuery's .unbind('mouseleave mouseover') but it doesn't remove them.

Is there any way how can I access all listeners from one category and remove them?

标签: javascriptjquery

解决方案


您可以将其替换为克隆:

const el = document.querySelector(...)
el.parentNode.replaceChild(el.cloneNode(true), el)

然后你可以重新连接你仍然需要的听过的东西(如果有的话)。


推荐阅读