<!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.
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