1. Overview
Although the deleted node is no longer in the document tree, it is actually still in memory and can be added to another location at any time.
When you traverse the child node of a parent node and perform a delete operation, please note that the child attribute is a read-only attribute and it will be updated in real time when the child node changes.
// Get the node to be deleted: var self = document.getElementById('to-be-removed');// Get the parent node: var parent = self.parentElement;// Delete: var removed = parent.removeChild(self); removed === self; // true2. example
<!DOCTYPE html><html><head></script></head><body><ul id="test-list"> <li>JavaScript</li> <li>Swift</li> <li>HTML</li> <li>ANSI C</li> <li>CSS</li> <li>DirectX</li></ul><script>var p= document.getElementById('test-list');var length = p.children.length;var i=0;for(; i<length; ){ var li = p.children[i]; var text = li.innerText; if(text!=='JavaScript' && text!=='HTML' && text!=='CSS'){ p.removeChild(li); alert(p.children.toString()); length--; }else{ i++; }}// Test:;(function () { var arr, i, t = document.getElementById('test-list'); if (t && t.children && t.children.length === 3) { arr = []; for (i = 0; i < t.children.length; i ++) { arr.push(t.children[i].innerText); } if (arr.toString() === ['JavaScript', 'HTML', 'CSS'].toString()) { alert('Test passed!'); } else { alert('Test failed: ' + arr.toString()); } } else { alert('Test failed!'); }})();</script></body></html>The above js removeChild method is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.