简体   繁体   English

removeEventListener of Anonymous function javaScript

[英]removeEventListener of Anonymous function javaScript

how can i remove this event listener I have tried but below code and it does not seam to bare any fruit我怎样才能删除这个事件侦听器我已经尝试过但低于代码并且它不会接缝露出任何果实

class Tag {
  constructor(name){
      this.tag = document.createElement(name);
  }
      removeEvent(e,func){
      this.tag.removeEventListener(e,func,false);
  }
      addEvent(e,func) {
      this.tag.addEventListener(e,func,false);
  }

}

let tag = new Tag();
tag.addEvent('click',(e)=> {
   console.log('something');
});

How do I get the removeEvent to work?如何让 removeEvent 工作? please help I specifically need how to reference the anonymous function since this works.请帮助我特别需要如何引用匿名 function 因为这有效。

 function handler(e){
     // code for event
 }

 tag.addEventListener('click',handler,false);  
 tag.removeEventlistener('click',handler,false);

I have tried adding我试过添加

  removeEvent(e,func) {
      func.ref = function (){
          return arguments.callee;
      }

      this.tag.removeEventListener(e,func.ref,false);

  }

Just doesn't work given now we would be referring to func.ref as the function reference;只是行不通,现在我们将 func.ref 称为 function 参考;

Simple way to remove anonymous event listener删除匿名事件监听器的简单方法

A nice and simple way I found to remove eventListener that uses anonymous functions is to add these two functions into your code:我发现删除使用匿名函数的 eventListener 的一个简单好方法是将这两个函数添加到您的代码中:

let events = new Map();
function addListener(element, event, callback, id) {
    events.set(id, callback);
    element.addEventListener(event, callback);
}

function removeListener(element, event, id) {
    element.removeEventListener(event, events.get(id));
    events.delete(id);
}

Anonymous function are great to keep the this context, and didn't found a good way to have both this , and the ability to remove the eventListener.匿名 function 非常适合保留this上下文,但没有找到同时拥有this和删除 eventListener 的能力的好方法。

Example例子

 let events = new Map(); function addListener(element, event, id, callback) { events.set(id, callback); element.addEventListener(event, callback); } function removeListener(element, event, id) { element.removeEventListener(event, events.get(id)); events.delete(id); } let btn = document.getElementById('btn'); let cpt = 1; addListener(btn, 'click', 'btnClick', e => { btn.innerHTML = `x${++cpt}`; if (cpt === 3) { removeListener(btn, 'click', 'btnClick'); } });
 <button id="btn">x1</button>

Anonymous functions can't be removed because they are not stored with an identifier that is accessible to your code. 无法删除匿名函数,因为它们不与您的代码可访问的标识符一起存储。 That's why they call them "anonymous" and that's one of the down sides to using them. 这就是为什么他们称他们为“匿名”,这是使用它们的不利方面之一。 Another down side is not being able to write unit tests directly for them. 另一个缺点是无法直接为它们编写单元测试。 The up side of them is that because they are not stored, a bit of memory is saved. 它们的优点是因为它们没有存储,所以节省了一点内存。 Also, JavaScript, being a functional programming language, allows for anonymous functions to be passed quite easily as data (although you can certainly pass named function references). 此外,JavaScript作为一种函数式编程语言,允许匿名函数作为数据很容易地传递(尽管你当然可以传递命名函数引用)。

There is not a way unfortunately to remove anonymous functions attached as listeners. 不幸的是,没有办法删除附加为侦听器的匿名函数。

You can use a little workaround storing every time the functions you pass in and keeping track of all the events, then use the "cached" event listeners for retrieving them and detach the events. 每次传入的函数和跟踪所有事件时,您都可以使用一些解决方法,然后使用“缓存”事件侦听器来检索它们并分离事件。

Here a draft of an example: 这是一个例子的草稿:

 var events = []; function storeEvent(id, fn, useCaptureMode, event) { var e = findStoredEvent(id, event, useCaptureMode); if (!e) { events.push({id, fn, useCaptureMode, event}); } } function findStoredEvent(id, event, useCaptureMode) { return events.find(el => el.id === id && el.event === event && el.useCaptureMode === el.useCaptureMode); } document.getElementById("test").addEventListener('click', function() { storeEvent(this.id, arguments.callee, false, 'click'); console.log('test'); }, false); function detachEvent() { var e = findStoredEvent('test', 'click', false); if (e) { document.getElementById("test").removeEventListener(e.event, e.fn, e.useCaptureMode); events.splice(events.findIndex(el => el === e), 1); } } 
 <button id="test"> Test </button> <button id="remove" onclick="detachEvent()"> Remove Event </button> 

I hope it helps 我希望它有所帮助

I had the same problem in an extension I was writing. 我在写作的扩展中遇到了同样的问题。 I solved this by adding a new listener that caught / stopped all propagation of the event before it reached the listener / function I wanted to remove. 我通过添加一个新的侦听器解决了这个问题,该侦听器在它到达我想删除的侦听器/函数之前捕获/停止了事件的所有传播。

window.addEventListener(type, function (event) {
    event.stopPropagation();
}, true);

Credit: StackOverflow 信用: StackOverflow

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

相关问题 JavaScript 中匿名函数的 removeEventListener - removeEventListener on anonymous functions in JavaScript 匿名函数bind的removeEventListener - removeEventListener of anonymous function bind removeEventListener和带参数的匿名函数 - removeEventListener and anonymous function with arguments Javascript“removeEventListener”没有对匿名函数做任何事情,没有简单的方法来做到这一点? - Javascript "removeEventListener" not doing anything with anonymous function, no easy way to do this? 如何使用匿名函数删除作为addEventListener的EventListener? - How to removeEventListener that is addEventListener with anonymous function? Javascript:具有匿名功能的RemoveEventListener - Javascript : RemoveEventListener with Anonym Function removeEventListener 不工作(函数不是匿名的,和 addEventListener 完全一样!!) - removeEventListener is not working (function is not anonymous, exactly the same as addEventListener!!) 带有部分功能的Javascript addEventListener和removeEventListener - Javascript addEventListener and removeEventListener with partial function removeEventListener 不工作,它不是匿名的 - the removeEventListener is not Working and it's not anonymous 使用addEventListener javascript添加后,removeEventListener()不会删除函数 - removeEventListener() not removing function after added with addEventListener javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM