【发布时间】:2020-05-16 18:55:59
【问题描述】:
如何正确移除事件监听器...
function createMaze() {
var x;
for (x = 0; x < 4; x++) {
var mazeBlock = document.createElement('div');
document.body.appendChild(mazeBlock);
mazeBlock.setAttribute('class', 'blockStyle');
mazeBlock.setAttribute('id', 'mazeBlock'+x);
mazeBlock.addEventListener( 'click', function(){ eventCall(this) } );
}
}
function eventCall(t) {
alert( t.id );
t.removeEventListener(); //...know that I'm missing something here.
// Also in my code, this remove will not happen here but be initiated somewhere else in the script.
}
我做了一堆digging 并且那里的最佳答案建议将侦听器添加到对象以便更容易删除但是...我不知道如何实现这一点
【问题讨论】:
标签: javascript