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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745271096a4619750.html
评论列表(0条)