原生js删除多个相同类名的子元素

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>demo</title>
</head>
<body>
    <div class="tempDiv"></div>
    <div class="tempDiv"></div>
    <div class="tempDiv"></div>
    <div class="tempDiv"></div>
    <div class="tempDiv"></div>
    <div class="tempDiv"></div>
</body>
</html>

 

function removeChild() {
        var child= document.getElementsByClassName("tempDiv");
        child.removeNode = [];
        if (child.length != undefined) {
            var len = child.length;
            for (var i = 0; i < len; i++) {
                child.removeNode.push({
                    parent: child[i].parentNode,
                    inner: child[i].outerHTML,
                    next: child[i].nextSibling
                });
            }
            for (var i = 0; i < len; i++){
                child[0].parentNode.removeChild(child[0]);
            }
        } else {
            child.removeNode.push({
                parent: child.parentNode,
                inner: child.outerHTML,
                next: child.nextSibling
            });
            child.parentNode.removeChild(child);
        }
    }

 

posted @ 2019-10-29 14:54  xiaoYu&  阅读(2258)  评论(0编辑  收藏  举报