Child element empty layer (the removeChild ())

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_32077521/article/details/97615716
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <meta charset="utf-8" />
</head>
<body>
    <style type="text/css">
        #div1 {
            width: 300px;
            height: 300px;
            border: 1px solid red;
        }
    </style>
    <input type="button" id="btnClear" value="清空" />
    <div id="div1">
        <button>哈哈</button>
        <button>哈哈</button>
        <button>哈哈</button>
        <button>哈哈</button>
        <button>哈哈</button>
        <button>哈哈</button>
    </div>
    <script type="text/javascript">
        window.onload = function () {
            document.getElementById('btnClear').onclick = function () {
                var div1 = document.getElementById('div1');
                while (div1.firstChild) {//firstchild属性是第一个子元素,自动向上替补,没有任何子元素时,返回undefined
                    div1.removeChild(div1.firstChild);//清空子元素
                }
            };
        };
    </script>
</body>
</html>

Guess you like

Origin blog.csdn.net/qq_32077521/article/details/97615716