简体   繁体   English

为什么我不能删除事件监听器?

[英]Why can't I remove my event listener?

I have an issue with removeEventListener, it doesn't seem to work at all, I've seen some other questions on this site but I don't get it, can you help me? 我的removeEventListener出现问题,似乎根本无法正常工作,我在此网站上看到了其他一些问题,但我不明白,您能帮我吗?

displayImg() {
    console.log('img')
    for (var i = 1; i <= 4; i++) {
      var line = "l"+i;
      var position = 0;
      var addDivLine = document.createElement('div');
      addDivLine.className = 'line';
      addDivLine.id = line;
      document.getElementById('container').appendChild(addDivLine);
      for (var j = 1; j <= 7; j++) {
        var block = "b"+j;
        var element = line+"-"+block;
        var addDivBlock = document.createElement('div');
        addDivBlock.className = 'block';
        addDivBlock.id = element;
        document.getElementById(line).appendChild(addDivBlock);
        memory.addEvent(element);
      };
    };

showImage(event) {
        event.preventDefault();
        memory.clickedBlock++;
        var block = event.target.id;
        memory.removeEvent(block);
}


addEvent(id){
    document.getElementById(id).addEventListener('click', function(){memory.showImage(event)});
  },
  removeEvent(id){
    console.log("remove");
    document.getElementById(id).removeEventListener('click', function(){memory.showImage(event)});
  },

I am creating div elements then put an eventListener on them, I call the same function to remove the event, I use the same id, is there something that I forgot? 我正在创建div元素,然后在它们上放一个eventListener,我调用了相同的函数来删除该事件,我使用了相同的ID,是否忘记了某些东西? I probably don't fully understand how it really works. 我可能不完全了解它是如何工作的。 Thanks a lot! 非常感谢!

The function looks like the same but its reference would be different. 该函数看起来相同,但其引用将有所不同。 So, define the function in a scope where it's available for both function and use the reference in both case. 因此,请在可同时用于两个函数的范围内定义函数,并在两种情况下均使用引用。

var callback =  function(){memory.showImage(event)};

addEvent(id){
    document.getElementById(id).addEventListener('click', callback);
}

removeEvent(id){
   console.log("remove");
   document.getElementById(id).removeEventListener('click', callback);
}

In this two lines: 在这两行中:

.addEventListener('click', function() { memory.showImage(event) });

and

.removeEventListener('click', function() { memory.showImage(event) });

function() { memory.showImage(event) } are two different functions. function() { memory.showImage(event) }是两个不同的函数。 You need to provide reference to the same function in both cases in order to bind/unbind listener. 在两种情况下,您都需要提供对同一功能的引用,以便绑定/取消绑定侦听器。 Save it so some variable and use in both places: 保存该变量以便在两个地方使用:

.addEventListener('click', memory.showImage);
.removeEventListener('click', memory.showImage);

For example using directly memory.showImage will work properly as it's the same function in both cases. 例如,直接使用memory.showImage可以正常工作,因为这两种情况下都是相同的功能。

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

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