2

12列のテーブルがあります。各列にはテキスト ボックスがあります。When I click on an 'Add' button, a new row should be inserted above the first row of the table. どうすればいいですか?たとえば、jquery append() を使用すると、最後に行が追加されるだけですが、最初の行の先頭に追加したいとします。

4

7 に答える 7

6

試す

$("#tableId tbody").prepend("<tr>..</tr>");
于 2013-08-16T09:31:11.797 に答える
3

Vanilla JavaScriptでは、実際には次のように単純です。

var firstRow = yourTable.rows[0];
firstRow.parentNode.insertBefore(newRow,firstRow);
于 2013-08-16T09:31:22.280 に答える
3

そのために次の代替手段を使用できます

1) .before() ドキュメントはここをクリック
2) .insertBefore()ドキュメントはここをクリック
3) .prepend()ドキュメントはここをクリック

于 2013-08-16T09:36:35.203 に答える
1

.prependを使用

$("#tableId tbody").prepend("tr code here");
于 2013-08-16T09:32:00.317 に答える
0

メソッドを使用し.prepend()ます。この関数は、まさに必要なことを行います。

于 2013-08-16T09:35:40.207 に答える
0

これを試して:

<table id="tblTest">
    <tr>
        <td><input type="text"/></td>
        <td><input type="text"/></td>
        <td><input type="text"/></td>
        <td><input type="text"/></td>
        <td><input type="text"/></td>
        <td><input type="text"/></td>
        <td><input type="text"/></td>
        <td><input type="text"/></td>
        <td><input type="text"/></td>
        <td><input type="text"/></td>
        <td><input type="text"/></td>
        <td><input type="text"/></td>
    </tr>
</table>
<table>
    <tr>
        <td><input type="button" value="Add" id="btnAdd"></input></td>
    </tr>
</table>


var row = $("#tblTest tbody").html();
$("#btnAdd").click(function(){
    $("#tblTest tbody").prepend(row);
});

フィドル

于 2013-08-16T09:35:59.513 に答える
0

次のようなものを使用できます。

$('table > tbody > tr:first').before('<tr>...</tr>');
于 2013-08-16T09:32:17.847 に答える