使用 JQuery 将数据插入 table 时定位 Tbody

Targeting Tbody while inserting data into a table using JQuery

我正在使用下面的代码将数据插入 table :

function addRow() {
    var key = $('#save_key').val();
    var qry = query();
    var table = document.getElementById("qrytbl");
    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);
    row.insertCell(-1).innerHTML = qry;
    row.insertCell(0).innerHTML = key;
}

My Table Structure

<table id=qrytbl>
                    <thead>
                        <tr>
                            <th>Key</th>
                            <th>Query</th>
                        </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>

代码将数据插入到 <thead> 标记内,而不是 <tbody> 内,这是我想要的。如何在我的代码中指定 <tbody> 路径?

$("#qrytbl tobdy").append("put data here");

您的代码中还有一个错误:将 Id 包裹在“”中,如下所示:

<table id="qrytbl">

是的,如果您输入正确的标记,这将插入到正确的行中:

这里是 fiddle:

http://jsfiddle.net/tt88g8of/

<table id="example">
    <thead>
        <tr>
            <td>Cell 1</td>
            <td>Cell 2</td>
        </tr>           
    </thead>        
    <tbody>        
    <tbody>       

</table>

<div class="clk">insert row</div>


$(".clk").on("click", function(){

    var data = "<tr> <td>CEll 1 data </td> <td>cell 2 data</td></tr>";

    $("#example tbody").append(data);
});