addEventListener('keydown', myFunction),
removeEventListener('keydown', myFunction)
怎么在myFunction传参数 this?
function myFunction(_this){
console.log(_this)
}
解决方法:
<body>
<button onclick="onRemoveEventListener()">removeEventListener</button>
<div class="container">123</div>
<script>
// 点击事件函数
const eventClickFunc = (e) => {
handleDivClick(e, this)
}
const container = document.querySelector('.container')
container.addEventListener('click', eventClickFunc)
// 监听 click
function handleDivClick(e, _this) {
console.log(e.target, _this);
}
// 取消监听 click
function onRemoveEventListener() {
console.log(1);
const container = document.querySelector('.container')
container.removeEventListener('click', eventClickFunc)
}
</script>
</body>