$('#my_table > tbody:last').append(newRow); // this will add new row inside tbody
$("table#myTable tr").last().after(newRow); // this will add new row outside tbody
//i.e. between thead and tbody
//.before() will also work similar
在补充 Nick Craver 的回答的同时,考虑到 rossisdead 提出的观点,如果场景的存在就像一个必须附加到一个空表,或者在某一行之前,我已经这样做了:
var arr = []; //array
if (your condition) {
arr.push(row.id); //push row's id for eg: to the array
idx = arr.sort().indexOf(row.id);
if (idx === 0) {
if (arr.length === 1) { //if array size is only 1 (currently pushed item)
$("#tableID").append(row);
}
else { //if array size more than 1, but index still 0, meaning inserted row must be the first row
$("#tableID tr").eq(idx + 1).before(row);
}
}
else { //if index is greater than 0, meaning inserted row to be after specified index row
$("#tableID tr").eq(idx).after(row);
}
}