javascript jQuery在第四行到最后一行之前添加行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8523860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:43:00  来源:igfitidea点击:

jQuery add row before fourth to last row

javascriptjqueryhtml-table

提问by leonel

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 jQueryshows 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.

这个线程在 jQuery添加表格行显示了如何在最后一行之后添加一行。我只需要修改它,在第四行到最后一行之前添加一行。

回答by Bassam Mehanni

how about you add a class to your grand total row

你在你的总计行中添加一个类怎么样

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

then in jquery you do

然后在 jquery 中你做

$('#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'

这样你就不是基于一个可能会改变的职位,而是基于一些有意义的事情,比如“总计”

回答by pimvdb

You want a negative .eq:

你想要一个否定.eq

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

回答by Didier Ghys

Use .before()instead of .after():

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

$('#myTable tr:last').before('<tr>...</tr><tr>...</tr>');

回答by epignosisx

You can get to the last row and then go up with prev()

您可以到达最后一行,然后使用prev()

$(function(){
   $("#myTable tr:last")
       .prev().prev().prev().prev()
       .after("<tr><td>x</td></tr>");
});