jQuery 空() 与删除()

发布于 2024-09-06 16:43:34 字数 117 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与他有关 2024-09-13 16:43:34
  • empty() 将清空所选内容,但保留所选内容本身。
  • remove() 将清空所选内容并删除所选内容本身。

考虑一下:

<div>
    <p><strong>foo</strong></p>
</div>

$('p').empty();  // --> "<div><p></p></div>"

// whereas,
$('p').remove(); // --> "<div></div>"

它们都删除了 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:

<div>
    <p><strong>foo</strong></p>
</div>

$('p').empty();  // --> "<div><p></p></div>"

// whereas,
$('p').remove(); // --> "<div></div>"

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:

街道布景 2024-09-13 16:43:34

文档解释得很好。它还包含示例:

before:

<div class="container">
  <div class="hello">Hello</div>
  <div class="goodbye">Goodbye</div>
</div>

.remove():

$('.hello').remove();

after:

<div class="container">
  <div class="goodbye">Goodbye</div>
</div>

before:

<div class="container">
  <div class="hello">Hello</div>
  <div class="goodbye">Goodbye</div>
</div>

.empty():

$('.hello').empty();

after:

<div class="container">
  <div class="hello"></div>
  <div class="goodbye">Goodbye</div>
</div>

就内存而言,一旦元素被移除DOM 并且不再有对其的引用,垃圾收集器将在运行时回收内存。

The documentation explains it very well. It also contains examples:

before:

<div class="container">
  <div class="hello">Hello</div>
  <div class="goodbye">Goodbye</div>
</div>

.remove():

$('.hello').remove();

after:

<div class="container">
  <div class="goodbye">Goodbye</div>
</div>

before:

<div class="container">
  <div class="hello">Hello</div>
  <div class="goodbye">Goodbye</div>
</div>

.empty():

$('.hello').empty();

after:

<div class="container">
  <div class="hello"></div>
  <div class="goodbye">Goodbye</div>
</div>

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.

三生池水覆流年 2024-09-13 16:43:34

时,它会删除 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 .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文