jQuery add row before fourth to last row

I have an invoice table. The last four rows are as follows, starting from last: Grand Total, Tax, Subtotal, Add a line link.

So I need to add a row before the "Add a link link row".

This thread Add table row in jQuery shows how to add a row after the last row. I just need to modify it, to add a row before the fourth to last row.


how about you add a class to your grand total row

<tr class="grand-total"></tr>

then in jquery you do

$('#myTable tr.grand-total').before('<tr></tr>');

this way you are not doing it based on a position that might be changing, but instead based on something meaningful like 'grand total'


你想要一个负面的.eq

$("#table tr").eq(-4).before(
    $("<tr>").append(
        $("<td>") // ...
    )
);

使用.before()而不是.after():

$('#myTable tr:last').before('<tr>...</tr><tr>...</tr>');
链接地址: http://www.djcxy.com/p/22982.html

上一篇: 如何在使用jQuery进行按钮单击时添加额外的html表格行?

下一篇: jQuery在第四行到最后一行之前添加行