JavaScript

超轻量级php框架startmvc

原生JavaScript实现remove()和recover()功能示例

更新时间:2020-07-17 18:18:01 作者:startmvc
本文实例讲述了原生JavaScript实现remove()和recover()功能。分享给大家供大家参考,具体如下:

本文实例讲述了原生JavaScript实现remove()和recover()功能。分享给大家供大家参考,具体如下:

利用原生JavaScript实现:

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

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


function remove(selectors) {
 selectors.removeNode = [];
 if (selectors.length != undefined) {
 var len = selectors.length;
 for (var i = 0; i < len; i++) {
 selectors.removeNode.push({
 parent: selectors[i].parentNode,
 inner: selectors[i].outerHTML,
 next: selectors[i].nextSibling
 });
 }
 for (var i = 0; i < len; i++)
 selectors[0].parentNode.removeChild(selectors[0]);
 }
 else {
 selectors.removeNode.push({
 parent: selectors.parentNode,
 inner: selectors.outerHTML,
 next: selectors.nextSibling
 });
 selectors.parentNode.removeChild(selectors);
 }
}
function recover(selectors) {
 var len = selectors.removeNode.length;
 for (var i = 0; i < len; i++) {
 var node = selectors.removeNode[i];
 if (node.next == null)
 node.parent.innerHTML += node.inner;
 else {
 var div = document.createElement("div");
 div.innerHTML = node.inner;
 node.parent.insertBefore(div.childNodes[0], node.next);
 }
 }
}

PS:关于javascript事件说明可参考本站javascript事件与功能说明大全:http://tools.jb51.net/table/javascript_event

原生JavaScript remove() recover()