向表格添加新行 - 然后向其添加内容

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script>
  5. function insRow(id) {
  6. var x = document.getElementById(id).insertRow(0);
  7. var y = x.insertCell(0);
  8. var z = x.insertCell(1);
  9. y.innerHTML = z.innerHTML = "New";
  10. }
  11. </script>
  12. </head>
  13.  
  14. <body>
  15. <table id="myTable" style="border: 1px solid black">
  16. <tr>
  17. <td>Row1 cell1</td>
  18. <td>Row1 cell2</td>
  19. </tr>
  20. <tr>
  21. <td>Row2 cell1</td>
  22. <td>Row2 cell2</td>
  23. </tr>
  24. <tr>
  25. <td>Row3 cell1</td>
  26. <td>Row3 cell2</td>
  27. </tr>
  28. </table>
  29.  
  30. <p>
  31. <input type="button" onclick="insRow('myTable')" value="插入行">
  32. </p>
  33.  
  34. </body>
  35. </html>