helloWolfs
helloWolfs
2021-01-15 23:32

removeEventListener无效

  • javascript

<!DOCTYPE html>

<html lang="en">

 

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>Document</title>

</head>

 

<body>

<button id="btn1">添加监听事件</button>

<button id="btn2">移除监听事件</button>

<div id="box3" style="width:150px;height:150px;background:pink"></div>

<script>

document.getElementById('btn1').addEventListener("click", fn3)

 

document.getElementById('btn2').onclick = function () {

document.getElementById('box3').removeEventListener("click", fn3)

}

 

function fn3() {

document.getElementById('box3').onclick = function () {

alert(11111)

}

}

</script>

</body>

 

</html>

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐

换一换