抱歉,如果这是一个常见问题,但我在搜索中找不到任何看起来相关的答案。
如果我像这样附加事件监听器:
window.addEventListener('scroll', function() { check_pos(box); }, false);
尝试稍后将其删除似乎不起作用,像这样:
window.removeEventListener('scroll', function() { check_pos(box); }, false);
我认为这是因为addEventListener和removeEventListener方法需要引用相同的函数,而我提供的是匿名函数,虽然代码相同,但它们不是完全相同的。
我该如何修改代码才能让调用removeEventListener函数起作用?“box”参数是一个我正在跟踪屏幕上的
window.addEventListener('scroll', function() { do_something('string1'); }, false);,稍后window.removeEventListener('scroll', function() { do_something('string1'); }, false);,然后window.addEventListener('scroll', function() { do_something('string2'); }, false);等等。 - Bungle