如何使用带参数的函数添加和删除事件侦听器? [英] How do I add and remove an event listener using a function with parameters?

查看:17
本文介绍了如何使用带参数的函数添加和删除事件侦听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果这是一个常见问题,但我无法通过搜索找到任何似乎相关的答案.

Sorry if this is a common question, but I couldn't find any answers that seemed pertinent through searching.

如果我像这样附加一个事件监听器:

If I attach an event listener like this:

window.addEventListener('scroll', function() { check_pos(box); }, false);

稍后尝试将其删除似乎不起作用,如下所示:

it doesn't seem to work to try to remove it later, like this:

window.removeEventListener('scroll', function() { check_pos(box); }, false);

我认为这是因为 addEventListenerremoveEventListener 方法想要引用相同的函数,而我为它们提供了匿名函数,虽然它们在代码,实际上并不相同.

I assume this is because the addEventListener and removeEventListener methods want a reference to the same function, while I've provided them with anonymous functions, which, while identical in code, are not literally the same.

如何更改我的代码以使对 removeEventListener 的调用正常工作?box"参数指的是我在屏幕上跟踪的 <iframe> 的名称;也就是说,我希望能够为我拥有的每个