<!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>
我想插入每次点击,插入一个li前面,然后是oUl.insertBefore(oLi,oLi [0]);选择插入oLi [0]为什么不死?是不是每次点击都创建一个li,然后在这个索引中插入第一个0,应该没问题?
答案 0 :(得分:0)
你误解了how insertBefore
works。您的{ to=/topics/lshekhar,
content_available=1,
collapse_key=sample,
delay_while_idle=true,
delivery_receipt_requested=true,
priority=10,
data={message={ "id" : "eARMS",
"submitter" : "lshekhar",
"topic" : "/topics/lshekhar"
}},
time_to_live=10000,
notification={"sound":"default"},
message_id=m-3319428685310488470,
badge=12}
元素甚至没有oLi
属性。相反,您需要传递[0]
才能在前面插入oUl.firstChild
。
oLi