问答详情
源自:9-13 插入节点insertBefore()

哪里错了,求指教(insertBefore相关内容)

<!DOCTYPE HTML>
<html>
<head>

<title>无标题文档</title>
</head>
<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].innerHTML == "HTML"){
             node = otest.chhildNodes[i];
             break;
         }
         i++;
  }  
 
otest.insertBefore(newNode,node);  
</script>
</body>
</html>

提问者:qq_伏特加_0 2016-09-18 16:50

个回答

  • 聋子听见哑巴说瞎子看到鬼3622774
    2016-09-18 21:44:15
    已采纳

      node = otest.chhildNodes[i];

    chhild 多打h 打错字注意检查不要乱挂

  • 3210
    2016-09-28 16:15:20

    while(otest.childNodes[i]){
             if(otest.childNodes[i].innerHTML == "HTML"){
                 node = otest.chhildNodes[i];
                 break;
             }
             i++;
      } 

    otest.childNodes[i].innerHTML == "HTML" 改为otest.childNodes[i].value== "HTML"


  • Quenyic
    2016-09-18 22:59:15

     node = otest.chhildNodes[i];