如何在JavaScript中使用removeEventListener()方法?
使用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 宝宝100日简短祝福语
17 初八的祝福语简短
18 祝福语女友文案简短霸气