元素销毁后,事件会自动取消绑定吗?

编程入门 行业动态 更新时间:2024-10-28 21:15:16
本文介绍了元素销毁后,事件会自动取消绑定吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

事件的元素被破坏后会发生什么?据我所知,jQuery在其全局存储库"中仍具有该事件.就我而言,创建了许多元素,并绑定了一个事件并再次销毁了它们.

What happens to the event when its element was destroyed? As far as I know, jQuery still has the event in its global "repository". In my case, a lot of elements are created, bound with an event and destroyed again...

使用过多后是否需要害怕一些内存问题?

Do I need to be scared of some memory problems after excessive use?

推荐答案

是.如果您有很多附加到html元素的事件处理程序,则应该显式删除它们(通过使用jQuery方法,例如remove或empty而不是例如detach或'regular'js),或更改事件的'binding ' 战略.我的意思是利用jQuery事件的委托.这样,您可以找到事件绑定元素集群的一些共同祖先,并将其绑定到事件.经验法则始终是尽量减少事件处理程序的数量.

Yes. If you have a lot of event handlers attached to html elements then you should either remove them explicitly (by using jQuery methods like remove or empty instead of eg detach or 'regular' js) or change your event 'binding' strategy. What I mean by that is taking advantage of jQuery's event's delegation. This way you can seek out some common ancestor to your cluster of event-binded-elements and binding it to the event instead. A rule of thumb is always to minimize the number of event handlers.

更多推荐

元素销毁后,事件会自动取消绑定吗?

本文发布于:2023-10-13 14:15:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1488178.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   元素   事件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!