admin管理员组文章数量:1417554
I looked at the jQuery
source code for the .empty()
function:
empty: function() {
for ( var i = 0, elem; (elem = this[i]) != null; i++ ) {
// Remove element nodes and prevent memory leaks
if ( elem.nodeType === 1 ) {
jQuery.cleanData( elem.getElementsByTagName("*") );
}
// Remove any remaining nodes
while ( elem.firstChild ) {
elem.removeChild( elem.firstChild );
}
}
Couldn't it be a lot simpler with just changing the innerHTML
to an empty string:
empty: function() {
for ( var i = 0, elem; (elem = this[i]) != null; i++ ) {
elem.innerHTML = "";
}
The empty
docs:
Description: Remove all child nodes of the set of matched elements from the DOM.
I looked at the jQuery
source code for the .empty()
function:
empty: function() {
for ( var i = 0, elem; (elem = this[i]) != null; i++ ) {
// Remove element nodes and prevent memory leaks
if ( elem.nodeType === 1 ) {
jQuery.cleanData( elem.getElementsByTagName("*") );
}
// Remove any remaining nodes
while ( elem.firstChild ) {
elem.removeChild( elem.firstChild );
}
}
Couldn't it be a lot simpler with just changing the innerHTML
to an empty string:
empty: function() {
for ( var i = 0, elem; (elem = this[i]) != null; i++ ) {
elem.innerHTML = "";
}
The empty
docs:
Share Improve this question edited May 5, 2012 at 23:58 Mike Samuel 121k30 gold badges227 silver badges254 bronze badges asked Apr 24, 2012 at 20:24 gdorongdoron 150k59 gold badges302 silver badges371 bronze badges 2Description: Remove all child nodes of the set of matched elements from the DOM.
-
It's explained by this ment in the code:
// Remove element nodes and prevent memory leaks
... – nnnnnn Commented Apr 24, 2012 at 21:28 - @nnnnnn. Yes, I'm actually asking how can it cause a memory leaks... – gdoron Commented Jun 6, 2012 at 22:26
1 Answer
Reset to default 11Just think about .data()
expandos and event handlers... By just removing the DOM, you would create memory leaks every time.
本文标签: javascriptWhy is the jquery empty function so complicatedStack Overflow
版权声明:本文标题:javascript - Why is the jquery empty function so complicated? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745271095a2650895.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论