Javascript Jquery在某个索引处向表中插入新行

Javascript Jquery在某个索引处向表中插入新行,javascript,jquery,html-table,Javascript,Jquery,Html Table,我知道如何使用jquery将新行追加或前置到表中: $('#my_table > tbody:last').append(html); 如何将行(在html变量中给出)插入特定的“行索引”I。因此,如果i=3,例如,该行将作为表中的第4行插入。尝试以下操作: $($('#my_table > tbody:last')[index]).append(html); var i = 3; $('#my_table > tbody > tr:eq(' + i + ')').

我知道如何使用jquery将新行追加或前置到表中:

$('#my_table > tbody:last').append(html);
如何将行(在html变量中给出)插入特定的“行索引”
I
。因此,如果
i=3
,例如,该行将作为表中的第4行插入。

尝试以下操作:

$($('#my_table > tbody:last')[index]).append(html);
var i = 3;

$('#my_table > tbody > tr:eq(' + i + ')').after(html);
或者这个:

var i = 3;

$('#my_table > tbody > tr').eq( i ).after(html);
var i = 4;

$('#my_table > tbody > tr:nth-child(' + i + ')').after(html);
或者这个:

var i = 3;

$('#my_table > tbody > tr').eq( i ).after(html);
var i = 4;

$('#my_table > tbody > tr:nth-child(' + i + ')').after(html);
所有这些都将使行处于相同的位置<代码>第n个子项使用基于1的索引。

您可以这样使用:

$('#my_table > tbody > tr').eq(i-1).after(html);
var arr = []; //array
if (your condition) {
  arr.push(row.id); //push row's id for eg: to the array
  idx = arr.sort().indexOf(row.id);

  if (idx === 0) {   
    if (arr.length === 1) {  //if array size is only 1 (currently pushed item)
        $("#tableID").append(row);
    }
    else {       //if array size more than 1, but index still 0, meaning inserted row must be the first row
       $("#tableID tr").eq(idx + 1).before(row);
    }   
  }          
  else {     //if index is greater than 0, meaning inserted row to be after specified index row
      $("#tableID tr").eq(idx).after(row);
  }    
}
索引是基于0的,因此要成为第四行,您需要
i-1
,因为
.eq(3)
将是第四行,您需要返回到第三行(
2
)并插入该行。

使用选择第n行(基于0)并在使用后添加您的行,因此:

如果html是
tr

请尝试以下操作:

$('#my_table tbody tr:nth-child(' + i + ')').after(html);
$("table#myTable tr").last().after(data);
注:


我知道它来得很晚,但对于那些希望纯粹使用
JavaScript
实现它的人来说,这里介绍了如何实现它:

  • 获取对单击的当前
    tr
    的引用
  • 创建一个新的
    tr
    DOM元素
  • 将其添加到引用的
    tr
    父节点
  • HTML:


    这将在单击按钮的行的正下方添加新表行

    再加上Nick Craver的回答,并考虑到RossiDead提出的观点,如果场景存在,比如必须附加到空表中,或者在某一行之前,我会这样做:

    $('#my_table > tbody > tr').eq(i-1).after(html);
    
    var arr = []; //array
    if (your condition) {
      arr.push(row.id); //push row's id for eg: to the array
      idx = arr.sort().indexOf(row.id);
    
      if (idx === 0) {   
        if (arr.length === 1) {  //if array size is only 1 (currently pushed item)
            $("#tableID").append(row);
        }
        else {       //if array size more than 1, but index still 0, meaning inserted row must be the first row
           $("#tableID tr").eq(idx + 1).before(row);
        }   
      }          
      else {     //if index is greater than 0, meaning inserted row to be after specified index row
          $("#tableID tr").eq(idx).after(row);
      }    
    }
    

    希望它能帮助别人

    这总是会导致索引超出范围异常。应该注意的是,如果jQuery的eq函数被赋予负值,它将循环到表的末尾。因此,运行此操作并尝试在0索引上插入它将导致在末尾插入新行,并添加
    .before(html)
    如果要在该索引之前插入新行,则此操作失败。如果表中没有行,是否有人有解决方案(基于索引)哪种方法对第一行也有效?@MartinM如果你在没有行的情况下插入,那么这是一个完全不同的场景(索引不重要?)-不管发生什么,你都想附加到末尾吗?@NickCraver,不,我只是在寻找一个基于索引的通用解决方案(像这样),但我的表在最后是空的..我可以,当然,如果表是空的,请检查并插入第一行,然后继续使用您的方法,但这对我来说似乎不太合适..不一样,但与