[英]Adding a new row above the first row of a table
我有一張12列的桌子。 每列將有一個文本框。 When I click on an 'Add' button, a new row should be inserted above the first row of the table
。 我該怎么辦? 說,如果我使用jquery append(),它將僅在末尾添加該行,但我想在第一行的頂部添加。
嘗試
$("#tableId tbody").prepend("<tr>..</tr>");
在Vanilla JavaScript中 ,它實際上非常簡單:
var firstRow = yourTable.rows[0];
firstRow.parentNode.insertBefore(newRow,firstRow);
使用.prepend
$("#tableId tbody").prepend("tr code here");
您可以使用如下形式:
$('table > tbody > tr:first').before('<tr>...</tr>');
使用.prepend()
方法。 該功能完全可以滿足您的需求。
嘗試這個:
<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);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.