向已有的table中插入数据

table:

<table id="seleted-table"
    class="table table-bordered table-hover"
    style="display: table">
    <thead>
        <tr>
            <th width="50%">#</th>
            <th width="50%">name</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

动态添加数据到table的第一行

if (tableIsEmpty) {
    $("#seleted-table").find('tbody').append("<tr><td>0</td><td>abc</td></tr>");
} else {
    $("#seleted-table").find('tbody tr:first').before("<tr><td>0</td><td>abc</td></tr>");
}

当table的body为空时,使用

$("#seleted-table").find('tbody tr:first').before("<tr><td>0</td><td>abc</td></tr>");

会把tr等加到<tbody>之前,所以当table为空是需要使用append。

更多请见:Add a row on top of table generated by Javascript

Add table row in jQuery

原文地址:https://www.cnblogs.com/drizzlewithwind/p/7219448.html