Detaches an event listener from all the provided targets.
- Use
Array.prototype.forEach()
andEventTarget.removeEventListener()
to detach the providedlistener
for the given eventtype
from alltargets
.
代码实现
const removeEventListenerAll = (
targets,
type,
listener,
options,
useCapture
) => {
targets.forEach(target =>
target.removeEventListener(type, listener, options, useCapture)
);
};
const linkListener = () => console.log('Clicked a link');
document.querySelector('a').addEventListener('click', linkListener);
removeEventListenerAll(document.querySelectorAll('a'), 'click', linkListener);
翻译自:https://www.30secondsofcode.org/js/s/remove-multiple-event-listeners