javascript - 无法删除事件监听器

标签 javascript addeventlistener

好的,我有一个带动画的 div:

var x = true;

function dynamicTaskbar(thumb) {
		function anim1() {
			thumb.style.backgroundColor = "green";
		}
		function anim2() {
			thumb.style.backgroundColor = "blue";
		}
		if (x === false) {
			thumb.style.backgroundColor = "red";
			thumb.removeEventListener("mouseover", anim1);
			thumb.removeEventListener("mouseleave", anim2);
      x = true;
		} else {
			thumb.style.backgroundColor = "blue";
			thumb.addEventListener("mouseover", anim1);
			thumb.addEventListener("mouseleave", anim2);
      x = false;
		}
	}

	//Create window's thumbnail for taskbar
	var thumbnail = document.createElement("div");
	thumbnail.setAttribute("class", "thumbnail");
	taskbar.append(thumbnail);
	taskbar.style.width = taskbar.style.width + thumbnail.style.width + "px";
	thumbnail.addEventListener("mousedown", function() {
		dynamicTaskbar(thumbnail);
	});
#taskbar {
  background-color: red;
  border: solid 1px black;
  
  width: 50px;
  height: 30px;
}

.thumbnail {
  width: 50px;
  height: 30px;
  border: solid 1px black;
}
<div id="taskbar"></div>

默认情况下,div 是红色的。

点击时:

  • 如果 x 为真,则变为假并且 div 变为蓝色。添加了两个事件监听器,mouseover(div 变为绿色)和 mouseleave(div 再次变为红色)。
  • 如果 x 为假,则它变为真并且 div 变为红色。但这是我的问题:两个事件监听器(mouseover 和 mouseleave)都应该被删除,但它不起作用。我在 Internet 上进行了搜索,但没有找到解决我的问题的方法。

有什么帮助吗?

最佳答案

此问题的解决方案是从 dynamicTaskbar() 函数中提取 anim1()anim2() 函数。

由于这两个函数都位于 dynamicTaskbar() 函数中,因此每次执行该函数时都会一次又一次地创建它们,从而导致实例与初始实例不同。

例如,如果在 dynamicTaskbar() 的第一次执行(第一次单击)中,anim1() 的“对象 ID”将为“1”,而在第二次执行中执行它将是“2”。因此,当您尝试删除监听器时,您实际上是在尝试为不同的对象引用删除它。

看例子:

var x = true;

function anim1(thumb) {
  thumbnail.style.backgroundColor = "green";
}
function anim2(thumb) {
  thumbnail.style.backgroundColor = "blue";
}
    
function dynamicTaskbar(thumb) {
		if (x === false) {
			thumbnail.style.backgroundColor = "red";
			thumbnail.removeEventListener("mouseover", anim1);
			thumbnail.removeEventListener("mouseleave", anim2);
      x = true;
		} else {
			thumbnail.style.backgroundColor = "blue";
			thumbnail.addEventListener("mouseover", anim1);
			thumbnail.addEventListener("mouseleave", anim2);
      x = false;
		}
	}

	//Create window's thumbnail for taskbar
	var thumbnail = document.createElement("div");
	thumbnail.setAttribute("class", "thumbnail");
	taskbar.append(thumbnail);
	taskbar.style.width = taskbar.style.width + thumbnail.style.width + "px";
	thumbnail.addEventListener("mousedown", function() {
		dynamicTaskbar(thumbnail);
	});
#taskbar {
  background-color: red;
  border: solid 1px black;
  
  width: 50px;
  height: 30px;
}

.thumbnail {
  width: 50px;
  height: 30px;
  border: solid 1px black;
}
<div id="taskbar"></div>

关于javascript - 无法删除事件监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53362326/

相关文章:

javascript - 在 d3.js v4 中重新绑定(bind)导出

javascript - 如何判断视频是否受版权保护? YouTube API v3

querySelector 的 javascript addEventListener 不起作用

javascript - 使用 document.addEventListener 破坏 document.getElementById

javascript - 只需使用 javascript 即可通过用户输入查询基于 JSON 的 API

javascript - 从平面数组构建一棵树

javascript - Select2 自定义渲染

javascript - Web 组件 :host:hover

javascript - 仅当宽度大于 x 像素时才执行 javascript 代码

javascript - 为什么该脚本的位置会改变网页的行为?