从内存中删除HTML元素(DOM节点) [英] Remove HTML element (DOM Node) from memory

查看:869
本文介绍了从内存中删除HTML元素(DOM节点)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 mdn文档,方法 removeChild 从DOM中删除一个节点,但它仍然驻留在内存中。
我的问题是我想从内存中删除它。
我尝试使用删除运算符,但对象仍然存在...

according to mdn documentation the method removeChild removes a node from the DOM but it still resides in memory. My problem is that I want to delete it from memory as well. I've tried with the delete operator but the object is still there...

myCanvas.parentElement.removeChild(myCanvas);  // myCanvas actually removed from DOM
delete myCanvas;  // false. does nothing
alert(myCanvas); // shows HTMLCanvasElement instead of undefined


推荐答案

阅读 http://perfectionkills.com/understanding-delete/ 。删除操作符不适用于变量(这就是为什么它返回 false )。

Read http://perfectionkills.com/understanding-delete/. The delete operator is not for variables (that's why it returns false).

如果要删除变量引用DOM节点,使用

If you want to remove the variable's reference to the DOM node, use

myCanvas = null;

覆盖该值。通常你不需要这样做,因为JS的垃圾收集器为你做的所有工作。

to overwrite the value. Usually you never need to do this, because the garbage collector of JS does all the work for you.

这篇关于从内存中删除HTML元素(DOM节点)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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