Relatively simple, for example: There is a region <div id= "Newbody" ></div>, and now requires that all nodes be deleted. I believe many people will write this:
Copy Code code as follows:
var Divpanel = document.getElementById ("Newbody");
var controlinfo= divpanel.childnodes;
for (var index = 0;index < controlinfo.length; index++)
{
Divpanel.removechild (Controlinfo[index]);
}
I see, yes ah, yes, this is true ah, one traversal, and finally all deleted. But, think about it, every time you delete a node, the controlinfo.length becomes smaller, so you can't delete it all-there are bugs. So how to improve it? It's already clear that since the start start doesn't work, we start at the end, completely right, as follows:
Copy Code code as follows:
var Divpanel = document.getElementById ("Newbody");
var controlinfo= divpanel.childnodes;
for (var index = controlinfo.length-1;index >= 0; index--)
{
Divpanel.removechild (Controlinfo[index]);
}
Commissioning, Very good!
This trap is not only encountered in JavaScript, basically all languages, such as C #, Java and so on, we will encounter such problems, sometimes because of such a problem and waste a lot of time. Already knew this question very early, but always too believed own memory, but did not have the good record, now the special record, warns oneself, simultaneously also shares with everybody.