javascript中创建插入元素

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script type="text/javascript">
        window.onload = function () {
            var oBtn = document.getElementById("btn");
            var oTxt = document.getElementById("txt1");
            var oUl = document.getElementById("ul1");
            oBtn.onclick = function () {
                var oLi = document.createElement("li");
                var aLi = oUl.getElementsByTagName("li");
                oLi.innerHTML = oTxt.value;
                if (aLi[0].length > 0) {
                    //如果之前有元素则插入在前面
                    oUl.insertBefore(oLi, ali[0]);
                }
                else {
                    oUl.appendChild(oLi);
                }
            }
        }
    </script>
</head>
<body>
    <input type="text" value="" id="txt1" />
    <input type="button" value="创建" id="btn" />
    <ul id="ul1"></ul>
</body>
</html>


--拓展
Oul.removeChild(this.parentNode);

  

原文地址:https://www.cnblogs.com/alphafly/p/3771320.html