简体   繁体   English

从元素 Javascript 中删除所有事件侦听器

[英]Remove all event listeners from an element Javascript

I am developing a shopify app and I have to build everything on a page with pure vannila javascript and faced a problem, on which at the time I can't figure out a fix.我正在开发一个 shopify 应用程序,我必须使用纯香草 javascript 在页面上构建所有内容,并且遇到了一个问题,当时我无法解决这个问题。

When the script is loaded, I assign event listeners like so:加载脚本后,我会像这样分配事件侦听器:

 sidebarLi.setAttribute('id', 'faq-li-' + section.id);
 let sidebarLink = document.createElement('a');

          sidebarLi.addEventListener('mouseover', function () {
          sidebarLi.style.backgroundColor = settings.sidebar_background_hover_color;
        });

        sidebarLi.addEventListener('mouseout', function () {
          sidebarLi.style.backgroundColor = settings.sidebar_inactive_background_color;
        });

        sidebarLink.addEventListener('mouseover', function () {
          sidebarLink.style.color = settings.sidebar_category_hover_color;
        });

        sidebarLink.addEventListener('mouseout', function () {
          sidebarLink.style.color = settings.sidebar_category_color;
        });

Then, later on, I need to remove them, tried using this trick:然后,稍后,我需要删除它们,尝试使用这个技巧:

This is just an example:这只是一个例子:

var old_element = document.getElementById("btn");
var new_element = old_element.cloneNode(true);
old_element.parentNode.replaceChild(new_element, old_element);

But the element this has all the event listeners I want to remove.但是这个元素有我想要删除的所有事件监听器。

How to simply remove those event listeners, it could be done one by one, the code lines is not the priority for now.如何简单的移除那些事件监听器,可以一个一个地做,代码行暂时不是重点。

Later on I need to remove from one element event listeners, and add those to another one, I play with active statuses and active element should not have any active event listeners, not active elements, should have them.稍后我需要从一个元素事件监听器中删除,并将它们添加到另一个,我玩活动状态和活动元素不应该有任何活动事件监听器,而不是活动元素,应该有它们。

I get those two elements like so:我得到了这两个元素:

let activeLi = document.getElementById('faq-li-' + currentlyActiveSection.id);
let newActiveLi = document.getElementById('faq-li-' + section.id);

The activeLi element should not have any event listeners. activeLi 元素不应该有任何事件监听器。 The newActiveLi element should have all the event listeners which didn't have any before. newActiveLi 元素应该包含以前没有的所有事件侦听器。

Any help will be appriciated, thanks.任何帮助将不胜感激,谢谢。

There is no direct API to remove all the event listeners on element.没有直接的 API 来删除元素上的所有事件侦听器。

As you mentioned, Clone and replacing element is one way.正如您所提到的,克隆和替换元素是一种方法。 Based on MDN documentation (cloneNode )基于 MDN 文档 (cloneNode )

Cloning a node copies all of its attributes and their values, including intrinsic (inline) listeners.克隆节点会复制其所有属性及其值,包括内在(内联)侦听器。 It does not copy event listeners added using addEventListener() or those assigned to element properties (eg, node.onclick = someFunction).它不会复制使用 addEventListener() 添加的事件侦听器或分配给元素属性的事件侦听器(例如,node.onclick = someFunction)。

Following snippet is see the behaviour.以下片段是查看行为。 After clicking "clone" button, the event listeners are not cloned with cloneNode .单击“克隆”按钮后,事件侦听器不会使用cloneNode进行克隆。

 const ele = document.getElementById('hello'); ele.addEventListener('click', () => console.log("Hello")); const clone = document.getElementById('clone'); clone.addEventListener('click', () => { const new_ele = ele.cloneNode(true); ele.parentNode.replaceChild(new_ele, ele); console.log('Cloned and replaced without events. No more saying Hello'); });
 #hello { border: 1px solid grey; margin-bottom: 40px; padding: 10px; display: inline-block; } #clone { border: 1px solid grey; display: inline-block; padding: 10px; color: red; }
 <div> <div id="hello"> Say Hello on Click </div> <div id="clone"> clone </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM