元素销毁后,事件会自动取消绑定吗? [英] Will events automatically be unbound after the element was destroyed?

查看:474
本文介绍了元素销毁后,事件会自动取消绑定吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

事件的元素被破坏后会发生什么?据我所知,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方法,例如removeempty而不是例如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.

这篇关于元素销毁后,事件会自动取消绑定吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆