I have an HTML table with a header and a footer:

我有一个带有页眉和页脚的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>

I am trying to add a row in tbody with the following:

我试图在tbody中添加一行如下:

myTable.insertRow(myTable.rows.length - 1);

but the row is added in the tfoot section.

但是在tfoot部分添加了行。

How to insert it tbody?

如何插入它的tbody?

8 个解决方案

#1


125

If you want to add a row into the tbody, get a reference to it and add it there.

如果您想要将一行添加到tbody中,请参考它并将其添加到该tbody中。

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

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

// 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);

A working demo here. Also you can check the documentation of insertRow here.

一个工作演示。您还可以在这里查看insertRow的文档。

更多相关文章

  1. 有没有办法强制Chrome在OSX上以演示模式打开?
  2. 真人演示乳房按摩法 咪咪大而不垂
  3. HTML高级标签之窗口分帧(前台界面演示)
  4. 如何使用jQuery在我的网站演示中自动填写表单,好像真人正在打字?
  5. javascript适合移动端的响应式瀑布流插件实例演示
  6. Web分页实现及实例演示(一)——subList()

随机推荐

  1. 感知机(python实现)
  2. conda不是内部或者外部命令
  3. 使用自定义qemu二进制文件与libvirt失败?
  4. MQTT协议应用:外网手机控制内网树莓派
  5. 没有这样的专栏:blog_article.date
  6. 在不知道序列长度的情况下,在Python中展开
  7. 开启我的Python量化之路
  8. 50. Pow(x,n) Leetcode Python
  9. 【机器学习算法-python实现】最大似然估
  10. 毕业设计-基于深度神经网络的语音关键词