我正在使用JavaScript动态创建表。我有一个最低限度的例子,我想要什么。这是要生成的HTML:
<table id='mainTable'>
<tr>
<td> Row 1 Cell 1 </td>
</tr>
<tr>
<td>
<table>
<tr>
<td> ... </td>
</tr>
</table>
</td>
</tr>
</table>
我通过var table1 = document.getElementById("mainTable");
获取主表,然后使用JavaScript insertRow(0)
和insertCell(0)
添加行和单元格。我只想知道如何在td单元格内添加新表。
答案 0 :(得分:1)
// create new table
const newTable = document.createElement('table')
// target cell, this is selecting every td inside your mainTable
const target = document.querySelectorAll('#mainTable td')
// add new Table to first cell (if you want to add to specific cell you can add a class to it and the target it like that) or change the number in brackets:
target[0].appendChild(newTable)