要在JavaScript中实现表格的行追加,你可以使用以下方法:
- 使用
insertRow()
方法在表格中插入新行。
- 使用
insertCell()
方法在行中插入新单元格。
- 使用
innerHTML
属性设置单元格的内容。
完整的代码示例:
<table id="myTable"> <tr> <th>Header 1</th> <th>Header 2</th> </tr> <tr> <td>Cell 1</td> <td>Cell 2</td> </tr> </table> <button onclick="addRow()">Add Row</button> <script> function addRow() { var table = document.getElementById("myTable"); var row = table.insertRow(); // 在表格末尾插入新行 var cell1 = row.insertCell(); // 在新行中插入第一个单元格 var cell2 = row.insertCell(); // 在新行中插入第二个单元格 cell1.innerHTML = "Cell 1"; // 设置第一个单元格的内容 cell2.innerHTML = "Cell 2"; // 设置第二个单元格的内容 } </script>当点击"Add Row"按钮时,将会在表格中添加一行,并填充两个单元格的内容为"Cell 1"和"Cell 2"。
提供PHP及ThinkPHP框架的定制开发、代码优化,PHP修改、ThinkPHP修改。
版权声明:除特别声明外,本站所有文章皆是本站原创,转载请以超链接形式注明出处!