如何使用JavaScript删除事件监听器?
使用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>热门推荐
10 俄语祝福语贺卡文案简短
11 新郎表白成语祝福语简短
12 提早庆祝生日祝福语简短
13 亲姐姐祝福语简短英文
14 九子订婚祝福语简短
15 六一互换礼物祝福语简短
16 给宝贝开学祝福语简短
17 日语送考祝福语简短
18 中式蛋糕开业祝福语简短