如何在Javascript中的HTML表体中插入行?

Jér*_*nge 112 html javascript row html-table insert

我有一个带有页眉和页脚的HTML表格:

<table id="myTable">
    <thead>
        <tr>
            <th>My Header</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>aaaaa</td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <td>My footer</td>
        </tr>
    <tfoot>
</table>
Run Code Online (Sandbox Code Playgroud)

我正在尝试tbody使用以下内容添加一行:

myTable.insertRow(myTable.rows.length - 1);
Run Code Online (Sandbox Code Playgroud)

但该行添加在该tfoot部分中.

如何插入tbody

Jon*_*uin 186

如果要在其中添加行tbody,请获取对它的引用并将其添加到那里.

var tableRef = document.getElementById('myTable').getElementsByTagName('tbody')[0];

// Insert a row in the table at the last row
var newRow   = tableRef.insertRow();

// Insert a cell in the row at index 0
var newCell  = newRow.insertCell(0);

// Append a text node to the cell
var newText  = document.createTextNode('New row');
newCell.appendChild(newText);
Run Code Online (Sandbox Code Playgroud)

一个工作演示在这里.您也可以查看insertRow 此处的文档.

  • 我的代码中断因为:无法读取null的属性"insertRow".有什么建议? (3认同)
  • 除了 `tableRef.insertRow(tableRef.rows.length);` 你可以简单地使用 `tableRef.insertRow(-1);` 在表格的末尾插入行。 (3认同)
  • @Vrankela:您的选择器可能不正确(示例中的第一行)。 (2认同)
  • 以上将第一次失败。而是检查是否没有行:var rows = tableRef.rows; var rowsSoFar =(rows == null)吗?0:行长度 var newRow = tableRef.insertRow(rowsSoFar); (2认同)

Sas*_*ran 22

您可以使用Jquery尝试使用代码段.

$(table).find('tbody').append("<tr><td>aaaa</td></tr>");
Run Code Online (Sandbox Code Playgroud)

  • 也许你是对的,但有时候你需要知道真正的东西:)而且正如我所说的,在使用任何库之前总是知道JavaScript没有硬感的身体;) (17认同)
  • 这有助于我,`find('tbody')`是对的. (2认同)

Kur*_*ula 9

基本方法:

这应该添加html格式的内容并显示新添加的行.

var myHtmlContent = "<h3>hello</h3>"
var tableRef = document.getElementById('myTable').getElementsByTagName('tbody')[0];

var newRow = tableRef.insertRow(tableRef.rows.length);
newRow.innerHTML = myHtmlContent;
Run Code Online (Sandbox Code Playgroud)

  • 我最喜欢的解决方案:非常优雅,并且增加了添加内部 HTML 的灵活性。 (3认同)

Tee*_*emu 8

你很接近,只需将行添加到tbody而不是table:

myTbody.insertRow();
Run Code Online (Sandbox Code Playgroud)

在使用之前获取对tBody(myTbody)的引用.请注意,您不需要传递表中的最后一个位置,在省略参数时它会自动定位在最后.

jsFiddle的现场演示.


小智 8

let myTable = document.getElementById('myTable').getElementsByTagName('tbody')[0];

let row = myTable.insertRow();
let cell1 = row.insertCell(0);
let cell2 = row.insertCell(1);
let cell3 = row.insertCell(2);

cell1.innerHTML = 1;
cell2.innerHTML = 'JAHID';
cell3.innerHTML = 23;

row = myTable.insertRow();
cell1 = row.insertCell(0);
cell2 = row.insertCell(1);
cell3 = row.insertCell(2);

cell1.innerHTML = 2;
cell2.innerHTML = 'HOSSAIIN';
cell3.innerHTML = 50;
Run Code Online (Sandbox Code Playgroud)
table {
  border-collapse: collapse;
}

td, th {
  border: 1px solid #000;
  padding: 10px;
}
Run Code Online (Sandbox Code Playgroud)
<table id="myTable">
    <thead>
        <tr>
            <th>ID</th>
            <th>NAME</th>
            <th>AGE</th>
        </tr>    
     </thead>
    <tbody></tbody>
</table>
Run Code Online (Sandbox Code Playgroud)


Mar*_*wan 5

我认为这个脚本正是你需要的

var t = document.getElementById('myTable');
var r =document.createElement('TR');
t.tBodies[0].appendChild(r)
Run Code Online (Sandbox Code Playgroud)


小智 5

添加行:

<html>
    <script>
        function addRow() {
            var table = document.getElementById('myTable');
            //var row = document.getElementById("myTable");
            var x = table.insertRow(0);
            var e = table.rows.length-1;
            var l = table.rows[e].cells.length;
            //x.innerHTML = "&nbsp;";

            for (var c=0, m=l; c < m; c++) {
                table.rows[0].insertCell(c);
                table.rows[0].cells[c].innerHTML = "&nbsp;&nbsp;";
            }
        }

        function addColumn() {
            var table = document.getElementById('myTable');
            for (var r = 0, n = table.rows.length; r < n; r++) {
                table.rows[r].insertCell(0);
                table.rows[r].cells[0].innerHTML = "&nbsp;&nbsp;";
            }
        }

        function deleteRow() {
            document.getElementById("myTable").deleteRow(0);
        }

        function deleteColumn() {
            // var row = document.getElementById("myRow");
            var table = document.getElementById('myTable');
            for (var r = 0, n = table.rows.length; r < n; r++) {
                table.rows[r].deleteCell(0); // var table handle
            }
        }
    </script>

    <body>
        <input type="button" value="row +" onClick="addRow()" border=0 style='cursor:hand'>
        <input type="button" value="row -" onClick='deleteRow()' border=0 style='cursor:hand'>
        <input type="button" value="column +" onClick="addColumn()" border=0 style='cursor:hand'>
        <input type="button" value="column -" onClick='deleteColumn()' border=0 style='cursor:hand'>

        <table  id='myTable' border=1 cellpadding=0 cellspacing=0>
            <tr id='myRow'>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
            </tr>
        </table>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

还有细胞。

  • 我认为您应该在创建示例时使用更合理的变量。 (7认同)

小智 5

您还可以使用querySelector选择正文,然后在其末尾插入新行。

用于append将 Node 或 DOMString 对象插入到新单元格中,然后将其插入到新行中。

var myTbody = document.querySelector("#myTable>tbody");
var newRow = myTbody.insertRow();
newRow.insertCell().append("New data");
Run Code Online (Sandbox Code Playgroud)
<table id="myTable">
  <thead>
    <tr>
      <th>My Header</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Data</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>My footer</td>
    </tr>
  </tfoot>
</table>
Run Code Online (Sandbox Code Playgroud)