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 = 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行,你需要回到第3行( 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屋!
查看全文