Adding anonymous event listeners to elements provides flexibility and simplicity, but when it's time to remove them, it can pose a challenge without replacing the element itself.
The question arises: can an anonymous event listener added in this manner be removed without altering the element?
<pre>
element.addEventListener(event, function(){/ do work here /}, false);
</pre>
Unfortunately, the answer is no. There's no way to remove an anonymous event handler cleanly unless a reference to the handler was stored at creation.
To address this issue, consider storing the event handlers in a central location, such as the main object of the page. This allows for easy iteration and clean disposal of handlers when required.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3