insertBefore , previousSibling
我下面代碼輸出不是應(yīng)該是在HTML前插入php?
哪里錯(cuò)了?
把ot,otest.childNodes[2].previousSibling 換成ot,otest.childNodes[1].previousSibling
輸出就是正確的
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>無(wú)標(biāo)題文檔</title>
</head>
<body>
<ul id="test"><li>JavaScript</li><li id="id1">HTML</li></ul>
<script type="text/javascript">
? var otest = document.getElementById("test"); ?
? var ote = document.getElementById("id1");
? var ot = document.createElement("li");
? ot.innerHTML ="php";
? otest.insertBefore(ot,otest.childNodes[2].previousSibling);
</script>?
</body>
</html>
2016-09-27
你自己不是找到錯(cuò)了嗎,otest.childNodes[2]是指第三個(gè)child啊,下標(biāo)從0開(kāi)始的