1. on事件类型
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button id="add">绑定事件</button>
<button id="remov">解除绑定</button>
<script>
const add = document.querySelector('#add')
const remov = document.querySelector('#remov')
add.onclick = function(){
console.log(1);
}
remov.onclick = function(){
add.onclick = null
}
</script>
</body>
</html>
2. addEventListener解除
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button id="add">绑定事件</button>
<button id="remov">解除绑定</button>
<script>
const add = document.querySelector('#add')
const remov = document.querySelector('#remov')
function fn() {
console.log(1);
}
add.addEventListener('click',fn)
remov.addEventListener('click',function(){
add.removeEventListener('click',fn)
})
</script>
</body>
</html>