【问题标题】:Create a html table where inputs are inside <td>创建一个输入在 <td> 内的 html 表
【发布时间】:2021-04-04 21:21:26
【问题描述】:

我有以下 html 表格:

<table>
    <tr>
      <td> <input> </td>
      <td> <input> </td>
      <td> <input> </td>
    </tr>
    <tr>
      <td> <input> </td>
      <td> <input> </td>
      <td> <input> </td>
    </tr>
    <tr>
      <td> <input> </td>
      <td> <input> </td>
      <td> <input> </td>
    </tr>
  </table>

相反,我想仅使用 javascript 创建此表。我已阅读以下post。但是,我希望在每个&lt;td&gt; 中都有一个&lt;input&gt;(如提供的代码所示)。任何指导或帮助将不胜感激。

谢谢!

【问题讨论】:

    标签: javascript html input html-table


    【解决方案1】:

    我找到了答案:

    const table = document.querySelector('table');
    
    function generateTable() {
      for (let i = 0; i < 5; i++) {
        const row = table.insertRow();
        for (let a = 0; a < 5; a++) {
          const cell = row.insertCell();
          const inputCell = document.createElement("INPUT");
          cell.appendChild(inputCell);
        }
      }
    }
    
    generateTable()
    &lt;table&gt; &lt;/table&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-27
      • 2012-08-26
      • 1970-01-01
      • 2013-06-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多