简体   繁体   English

JS:隐藏元素上的事件侦听器

[英]JS: event listener on hidden element

Do I need to removeEventListener when I change element's style display:none ? 更改元素的样式display:none时,是否需要removeEventListener display:none
Do events effect page performance? 事件会影响页面性能吗?

No you don't actually need to , specially if that element is going to be displayed again, because it wouldn't be cool to add/remove the listener every single time. 不, 您实际上不需要 ,特别是如果该元素将再次显示时,因为每次添加/删除侦听器并不是一件很酷的事情。 Event listeners are asynchronous, and only impact on performance when they are executed, so since that you're hiding that element (and the user isn't able to interact with it) your listener will not be called as long as it stays hidden, and there will not be any performance impact . 事件侦听器是异步的,只会在执行时影响性能,因此, 由于您正在隐藏该元素(并且用户无法与之交互) ,只要侦听器保持隐藏状态, 就不会对其进行调用并且不会对性能产生任何影响

Plus: even if you were completely removing that element using parentEl.removeChild(childEl) , you still wouldn't have needed to remove the listeners, because deleting an element will cause all its listeners to be removed as well. 另外:即使您使用parentEl.removeChild(childEl)完全删除了该元素,您仍然不需要删除侦听器,因为删除元素也会导致其所有侦听器也被删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM