Jquery 在表中某个索引处插入新行 [英] Jquery insert new row into table at a certain index
本文介绍了Jquery 在表中某个索引处插入新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道如何使用 jquery 将新行附加或添加到表中:
I know how to append or prepend a new row into a table using jquery:
$('#my_table > tbody:last').append(html);
如何将行(在 html 变量中给出)插入特定的行索引"i
.因此,如果 i=3
,例如,该行将作为表中的第 4 行插入.
How to I insert the row (given in the html variable) into a specific "row index" i
. So if i=3
, for instance, the row will be inserted as the 4th row in the table.
推荐答案
You can use .eq()
and .after()
like this:
$('#my_table > tbody > tr').eq(i-1).after(html);
索引是基于 0 的,因此要成为第 4 行,您需要 i-1
,因为 .eq(3)
将是第 4 行,您需要回到第三行 (2
) 并插入 .after()
那个.
The indexes are 0 based, so to be the 4th row, you need i-1
, since .eq(3)
would be the 4th row, you need to go back to the 3rd row (2
) and insert .after()
that.
这篇关于Jquery 在表中某个索引处插入新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文