<body> <ul id="test"><li>JavaScript</li><li>HTML</li></ul> <script type="text/javascript"> var otest = document.getElementById("test"); var node;//内容为HTML的标签 var newNode = document.createElement("li"); newNode.innerHTML = "PHP"; var i = 0; while(otest.childNodes[i]){ if(otest.childNodes[i].nodeValue == "HTML"){ node = otest.chhildNodes[i]; break; } i++; } otest.insertBefore(newNode,node); </script> </body>
while(otest.childNodes[i]){ if(otest.childNodes[i].nodeValue == "HTML"){ // .nodeValue 改成 .innerHTML node = otest.chhildNodes[i]; // chhildNodes 改成 childNodes break; } i++; }
node.Value改成node.innerHTML 因为“HTML”是文本内容
第13行打错了,不是chhildNodes