javascript - Why is it useless to insert a child node before this label after CreateElement?
高洛峰
高洛峰 2017-06-17 09:16:50
0
2
846
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <input id="wb" type="text" />
    <input id="an" type="button" value="按钮" />
    <ul id="ul1">
        
    </ul>
    <script>
        window.onload=function()
        {
            var oWb = document.getElementById("wb");
            var btn = document.getElementById("an");
            var oUl = document.getElementById("ul1");
            btn.onclick=function()
            {
                var oLi = document.createElement("li");
               
                oLi.innerHTML = oWb.value;
                oUl.insertBefore(oLi,oLi[0]);   
              
            }
        }
    </script>
</body>
</html>

The last sentence oUl.insertBefore(oLi,oLi[0]); Why can’t I choose to insert at oLi[0]? I want it to be inserted at the front every time I click? Instead of creating a li every time you click, and then inserting it before the 0th index, it should be no problem.

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
小葫芦
node.insertBefore(newnode,existingnode);

The second parameter indicates inserting newnode before this node. If it is null or undefined, the insertBefore method will add newnode to the end, which has the same effect as appendChild

oLi[0]值为undefined
黄舟
location.href = "../exit.html";
throw new Error('stop');
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template