使用removeEventListener()JavaScript中的方法可删除addEventListener()方法附带的事件处理程序。
<!DOCTYPE html>
<html>
<head>
<style>
#box {
background-color: gray;
border: 2px dashed;
}
</style>
</head>
<body>
<div id="box">Demo Text!
<p>Click below to remove event handler.</p>
<button onclick="removeEvent()" id = "btnid">Remove</button>
</div>
<p id="pid"></p>
<script>
document.getElementById("box").addEventListener("mousemove", display);
function display() {
document.getElementById("pid").innerHTML = Math.random();
}
function removeEvent() {
document.getElementById("box").removeEventListener("mousemove", display);
}
</script>
</body>
</html>