jQuery 空() 与删除()
jQuery
中的 empty()
和 remove()
方法有什么区别,当我们调用这些方法中的任何一个时,所创建的对象都会被销毁并释放内存?
What's the difference between empty()
and remove()
methods in jQuery
, and when we call any of these methods, the objects being created will be destroyed and memory released?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
empty()
将清空所选内容,但保留所选内容本身。remove()
将清空所选内容并删除所选内容本身。考虑一下:
它们都删除了 DOM 对象,并且应该释放它们占用的内存,是的。
以下是文档链接,其中还包含示例:
empty()
will empty the selection of its contents, but preserve the selection itself.remove()
will empty the selection of its contents and remove the selection itself.Consider:
Both of them remove the DOM objects and should release the memory they take up, yes.
Here are links to documentation, which also contains examples:
文档解释得很好。它还包含示例:
before:
.remove():
after:
before:
.empty():
after:
就内存而言,一旦元素被移除DOM 并且不再有对其的引用,垃圾收集器将在运行时回收内存。
The documentation explains it very well. It also contains examples:
before:
.remove():
after:
before:
.empty():
after:
As far as memory is concerned, once an element is removed from the DOM and there are no more references to it the garbage collector will reclaim the memory when it runs.
时,它会删除 body 标记内的 HTML DOM 元素
$("body").empty()
--当您声明$("body").remove() > - 它删除整个 HTML DOM 以及 body TAG 。
$("body").empty()
-- it' removes the HTML DOM elements inside the body tag -when you declare
$("body").remove()
- it remove the entire HTML DOM along with body TAG .