2017-10-16 3 views
0

tbodyの下に新しい行を作成したいのですが、最初の行("aaaaa")の後に作成します。テーブル内の最初の行をスキップして動的行を作成する方法

<html> 
    <body onload="generate()"> 
     <table id="myTable"> 
      <thead> 
       <tr> 
        <th>My Header</th> 
       </tr> 
      </thead> 
      <tbody id="myList"> 
       <tr> 
        <td>aaaaa</td> 
       </tr> 
      </tbody> 
     </table> 
    </body> 
    <script> 
     function generate(){    
      var node = document.createElement("tr"); 
      node.setAttribute("id","one"); 
      var textnodeTD = document.createElement("td"); 
      var values = document.createTextNode("AAAAA"); 
      document.getElementById("myList").lastChild(node); 
      document.getElementById("one").appendChild(textnodeTD); 
      textnodeTD.appendChild(values); 
     } 
    </script> 
</html> 

答えて

3

変更lastChild

appendChildから

function generate() { 
 
    var node = document.createElement("tr"); 
 
    node.setAttribute("id", "one"); 
 
    var textnodeTD = document.createElement("td"); 
 
    var values = document.createTextNode("AAAAA"); 
 
    document.getElementById("myList").appendChild(node); 
 
    document.getElementById("one").appendChild(textnodeTD); 
 
    textnodeTD.appendChild(values); 
 
}
<body onload="generate()"> 
 
    <table id="myTable"> 
 
    <thead> 
 
     <tr> 
 
     <th>My Header</th> 
 
     </tr> 
 
    </thead> 
 
    <tbody id="myList"> 
 
     <tr> 
 
     <td>aaaaa</td> 
 
     </tr> 
 
    </tbody> 
 
    </table> 
 
</body>

+0

私はそれを得たありがとう.. – Sathish

関連する問題