如何清除使用jQuery从内存创建的DOM元素? [英] How to clear DOM elements created with jQuery from memory?

查看:96
本文介绍了如何清除使用jQuery从内存创建的DOM元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序正在使用jQuery来添加大量的DOM元素。所有这些元素都被放在一个主容器div中。

My app is using jQuery to add lots of DOM elements on the fly. All these elements are put into a main container div.

然后,每次显示一个新页面时,这个容器div将被删除,使用jQuery的remove();
问题是,似乎这个容器div的所有孩子似乎都被列在Chrome的devtools时间轴的节点下。

Then, every time a new page is shown, this container div is removed using jQuery's remove(); The problem is, it seems that all the children of this container div still seem to be listed under "Nodes" in Chrome's devtools timeline.

运行我的应用程序,我创建的DOM元素越多!但是当查看HTML源代码时,节点不存在。

The longer I run my app, the more DOM elements I'm creating! But when looking at the HTML source, the nodes aren't there.

我的代码:

    // creating lots of jquery elements and appending them to maincontainer
    var elm = document.createElement("DIV");
    var jq = jQuery(elm).text("hello");
    maincontainer.append(jq);

    // then, the main container(also a jquery object) is removed using
    maincontainer.remove();

从内存中清除DOM节点有什么问题?为什么它们保留在节点下?

What is wrong with clearing DOM nodes from memory this way? Why are they retained under "nodes"?

推荐答案

从页面中删除元素后,只需清除变量即可内部存储的元素也被清除。如果元素存储在 divElements 变量中,

After removing the elements from page, just clear the variables also.. so the elements which are stored internally also gets cleaned.. If the element is stored in divElements variable,

divElements.remove();
divElements = null;

我不知道这可能是您的解决方案,但它在我的应用程序中有效。 。

I don't know it may a solution for you... but it works in my application..

这篇关于如何清除使用jQuery从内存创建的DOM元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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