我知道如何使用JQuery在表格中添加新行:
var newRow = $("<tr>..."</tr>");
$("#mytable tbody").append(newRow);
问题是如何创建一个在某个现有行之前的新行。
我知道如何使用JQuery在表格中添加新行:
var newRow = $("<tr>..."</tr>");
$("#mytable tbody").append(newRow);
问题是如何创建一个在某个现有行之前的新行。
var newRow = $("<tr>...</tr>");
$("#idOfRowToInsertAfter").after(newRow);
关键在于知道要插入新行的行id,或至少想出一些选择器语法,以获取该行。
where_you_want_it.before(newRow)
或者
newRow.insertBefore(where_you_want_it)
-- MarkusQ
不是这样的:
$("#mytable tbody").append(newRow);
你将要做的事情类似于这样:
$("#id_of_existing_row").after(newRow);
使用:
var newTr = $('<tr>[...]</tr>');
您可以...
Insert it after (or before if you so choose) another row for which you know an ID (or whatever other property):
$('#<id of the tr you want to insert the new row after>').after(newTr)
Insert it after a particular row index (indices are 0-based, not 1-based):
$($('table#<id> tr')[<index>]).after(newTr)
…or as you mentioned, the absolute middle is possible:
var existingTrs = $('table#<id> tr')
$(existingTrs[parseInt(existingTrs.length / 2)]).after(newTr)
<td> <img class=\"insertRow\" src=\"/images/imgInsertRow.jpg\" /> </td>
$('table td img.insertRow').click(function(){
var newRow=$('<tr>........</tr>');
$(this).parent().parent().after(newRow);
});