如何使用JavaScript DOM向表中添加行?
要使用JavaScript向表中添加行,请使用DOM方法insertRow()
。
示例
您可以尝试运行以下代码,以了解如何一次向表中添加一行-
<!DOCTYPE html> <html> <head> <script> function rowFunction(x) { var a = document.getElementById(x).insertRow(0); var b = a.insertCell(0); var c = a.insertCell(1); b.innerHTML = c.innerHTML = "row"; } </script> </head> <body> <table style="border:2px solid black" id="newtable"> <tr> <td>One</td> <td>Two</td> </tr> <tr> <td>Three</td> <td>Four</td> </tr> <tr> <td>Five</td> <td>Six</td> </tr> </table> <p> <input type="button" onclick="rowFunction('newtable')" value="Add a row"> </p> </body> </html>