如何在事件监听器中传递 'this' 参数?-灵析社区

拽嘻嘻

addEventListener('keydown', myFunction), removeEventListener('keydown', myFunction) 怎么在myFunction传参数 this? function myFunction(_this){ console.log(_this) } **解决方法:** removeEventListener 123 // 点击事件函数 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) }

阅读量:18

点赞量:0

问AI
全能人才
function myFunction(_this, e) { console.log(_this, e); } document.querySelector("#i").addEventListener("keydown", function (event) { myFunction(this, event); }); "image.png" (https://wmlx-new-image.oss-cn-shanghai.aliyuncs.com/images/20241023/73108e024f5be48e95d53c01ca7bb55e.png)