jQuery中的 empty()和 remove()方法有什么不同,当我们调用这些方法中的任何一个时,创建的对象将被销毁,内存将被释放?
jQuery
empty()
remove()
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>
.empty():
$('.hello').empty();
<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.
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:
$("body").empty() -- it' removes the HTML DOM elements inside the body tag -
$("body").empty()
when you declare $("body").remove() - it remove the entire HTML DOM along with body TAG .
$("body").remove()