<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>无标题文档</title> </head> <body> <ul id="test"><li>JavaScript</li><li>HTML</li></ul> <script type="text/javascript"> var otest = document.getElementById("test"); var newNode = document.createElement("li"); newNode.innerHTML ="PHP"; for(var i=0;i<otest.childNotes.length;i++){ if(otest.childNotes[i].innerHTML=="HTML"){ otest.insertBefore(newNode,otest.childNodes[i]); } } </script> </body> </html>
childNotes改为childNodes
for(var i=0;i<otest.childNotes.length;i++){
if(otest.childNotes[i].innerHTML=="HTML"){
otest.insertBefore(newNode,otest.childNodes[i]);
}
这里面有两处拼写错误,应该是childNodes而不是childNotes