【问题标题】:How to create a table inside another table using JavaScript?如何使用 JavaScript 在另一个表中创建一个表?
【发布时间】:2019-05-14 16:58:26
【问题描述】:

我正在使用 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"); 获取主表,然后使用JavaScripts insertRow(0)insertCell(0) 添加行和单元格。我只想知道如何在 td 单元格中添加一个新表。

【问题讨论】:

    标签: javascript html dynamic html-table


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-05
      • 1970-01-01
      • 1970-01-01
      • 2014-12-26
      • 2014-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多