浅谈原生JavaScript实现remove()和recover()
1 function remove(selectors) { 2 selectors.removeNode = []; 3 if (selectors.length != undefined) { 4 var len = selectors.length; 5 for (var i = 0; i < len; i++) { 6 selectors.removeNode.push({ 7 parent: selectors[i].parentNode, 8 inner: selectors[i].outerHTML, 9 next: selectors[i].nextSibling 10 }); 11 } 12 for (var i = 0; i < len; i++) 13 selectors[0].parentNode.removeChild(selectors[0]); 14 } 15 else { 16 selectors.removeNode.push({ 17 parent: selectors.parentNode, 18 inner: selectors.outerHTML, 19 next: selectors.nextSibling 20 }); 21 selectors.parentNode.removeChild(selectors); 22 } 23 } 24 function recover(selectors) { 25 var len = selectors.removeNode.length; 26 for (var i = 0; i < len; i++) { 27 var node = selectors.removeNode[i]; 28 if (node.next == null) 29 node.parent.innerHTML += node.inner; 30 else { 31 var div = document.createElement("div"); 32 div.innerHTML = node.inner; 33 node.parent.insertBefore(div.childNodes[0], node.next); 34 } 35 } 36 }