浅谈原生JavaScript实现remove()和recover()

利用原生JavaScript实现:

1、remove(selectors)删除指定的一个或一组元素。

2、recover(selectors)恢复刚才删除的元素。

 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 }  

 

posted @ 2018-06-28 11:54  秀&莹&锐  阅读(2304)  评论(0编辑  收藏  举报