Javascript 在表的中间插入多个TDs

Javascript 在表的中间插入多个TDs,javascript,jquery,html,Javascript,Jquery,Html,我有一个2-4列9行的表。一些非常正确的列有一个文本(abc),一些有一个图像,一些两者都有,一些可能两者都没有 我想把第二列的内容复制到3个新创建的列中,但是把它们插入中间(在代码> ABC < /代码>和图像,如果适用的话)。所以看起来是这样的: 这种方法: $("table td:nth-child(2) [id$=2]").each(function(i) { var $newCell = $(this).wrap('<td></td>').parent()

我有一个2-4列9行的表。一些非常正确的列有一个文本(abc),一些有一个图像,一些两者都有,一些可能两者都没有

我想把第二列的内容复制到3个新创建的列中,但是把它们插入中间(在代码> ABC < /代码>和图像,如果适用的话)。所以看起来是这样的:

这种方法:

$("table td:nth-child(2) [id$=2]").each(function(i) {
  var $newCell = $(this).wrap('<td></td>').parent();
  var $newRow = $("table td:nth-child(2) [id$=1]").eq(i).parents('tr');
  $(this).parents('tr').remove();
  if ($newRow.find("td:contains('abc')").index() > -1) {
    $newRow.find("td:contains('abc')").before($newCell);
  } else if ($newRow.find("td.img").index() > -1) {
    $newRow.find("td:contains('img')").before($newCell);
  } else {
    $newRow.find("td:contains('img')").before($newCell);
  }
});

$("table td:nth-child(2) [id$=3]").each(function(i) {
  var $newCell = $(this).wrap('<td></td>').parent();
  var $newRow = $("table td:nth-child(2) [id$=1]").eq(i).parents('tr');
  $(this).parents('tr').remove();
  if ($newRow.find("td:contains('abc')").index() > -1) {
    $newRow.find("td:contains('abc')").before($newCell);
  } else if ($newRow.find("td.img").index() > -1) {
    $newRow.find("td:contains('img')").before($newCell);
  } else {
    $newRow.find("td:contains('img')").before($newCell);
  }
});
$(“表td:n子(2)[id$=2])。每个(函数(i){
var$newCell=$(this.wrap(“”).parent();
var$newRow=$(“表td:nth child(2)[id$=1])).eq(i).parents('tr');
$(this.parents('tr').remove();
if($newRow.find(“td:contains('abc')”)).index()>-1){
$newRow.find($newCell)之前的($td:contains('abc')));
}else if($newRow.find(“td.img”).index()>-1){
$newRow.find($newCell)之前的($td:contains('img')));
}否则{
$newRow.find($newCell)之前的($td:contains('img')));
}
});
$(“表td:n子(2)[id$=3]”)。每个(函数(i){
var$newCell=$(this.wrap(“”).parent();
var$newRow=$(“表td:nth child(2)[id$=1])).eq(i).parents('tr');
$(this.parents('tr').remove();
if($newRow.find(“td:contains('abc')”)).index()>-1){
$newRow.find($newCell)之前的($td:contains('abc')));
}else if($newRow.find(“td.img”).index()>-1){
$newRow.find($newCell)之前的($td:contains('img')));
}否则{
$newRow.find($newCell)之前的($td:contains('img')));
}
});
生成以下结果:


.

要实现这一点,您可以使用
:第n个子项在每第三行上循环,将所需的
td
从每一行添加到集合中的第一行。从那里您可以
删除()
不需要的行,如下所示:

for (var i = 0; i < $('table tr').length / 3; i++) {
    var $rows = $('table tr:nth-child(3n+' + i + ')');
    $rows.not(':first').addClass('remove').find('td:eq(1)').insertAfter($rows.first().find('td:eq(1)'));
}
$('table .remove').remove();
for(变量i=0;i<$('table tr')。长度/3;i++){
var$rows=$('table tr:nth child(3n+'+i+'));
$rows.not(':first').addClass('remove').find('td:eq(1)).insertAfter($rows.first().find('td:eq(1));
}
$('table.remove').remove();

要实现这一点,您可以使用
:第n个子项在第三行循环,将所需的
td
从每一行添加到集合中的第一行。从那里您可以
删除()
不需要的行,如下所示:

for (var i = 0; i < $('table tr').length / 3; i++) {
    var $rows = $('table tr:nth-child(3n+' + i + ')');
    $rows.not(':first').addClass('remove').find('td:eq(1)').insertAfter($rows.first().find('td:eq(1)'));
}
$('table .remove').remove();
for(变量i=0;i<$('table tr')。长度/3;i++){
var$rows=$('table tr:nth child(3n+'+i+'));
$rows.not(':first').addClass('remove').find('td:eq(1)).insertAfter($rows.first().find('td:eq(1));
}
$('table.remove').remove();

谢谢。也可以通过后缀来区分行:| 1,| 2,| 3,而不是每第三行循环一次?是的,这也可以,尽管会使代码更复杂。类似于
$(“表td:nth child(2)[id$=2]”)和
$(“表td:nth child(2)[id$=3])
?在我的实际例子中,我需要依赖后缀,因为有更多的行(没有后缀),不应该循环。我明白了。在这种情况下,请在问题中添加一个真实HTML示例,或者甚至开始一个新问题,因为这是对逻辑的一个相当重要的改变。好的,我接受了你的答案,因为它在这种情况下有效。我还提出了一个新问题:谢谢。也可以通过后缀来区分行:| 1,| 2,| 3,而不是每第三行循环一次?是的,这也可以,尽管会使代码更复杂。类似于
$(“表td:nth child(2)[id$=2]”)和
$(“表td:nth child(2)[id$=3])
?在我的实际例子中,我需要依赖后缀,因为有更多的行(没有后缀),不应该循环。我明白了。在这种情况下,请在问题中添加一个真实HTML示例,或者甚至开始一个新问题,因为这是对逻辑的一个相当重要的改变。好的,我接受了你的答案,因为它在这种情况下有效。我还提出了一个新问题: