浅谈原生JavaScript实现remove()和recover()
Posted 秀&莹&锐
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了浅谈原生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 }
以上是关于浅谈原生JavaScript实现remove()和recover()的主要内容,如果未能解决你的问题,请参考以下文章