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
jQuery add row before fourth to last row
提问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.
回答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
回答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>");
});