首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >复制多个Tds

复制多个Tds
EN

Stack Overflow用户
提问于 2016-06-07 15:13:29
回答 1查看 56关注 0票数 1

我有一个3列6行的表。我希望将其中一些内容复制到新创建的列中,但将它们插入中间。所以看起来是这样:

有了这种蟑螂:

代码语言:javascript
复制
$("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();
  $newRow.append($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();
  $newRow.append($newCell);
});

我知道结果是:

因此,新列应该插入到非常左边的列和带有Abc的列之间,而不是在最右边。

小提琴

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-07 16:07:14

要在表的中间插入一些内容,您可以使用before()after()。例如,我们希望在Abc项之前插入新的单元格,所以您可以使用下面的代码在Abc (最后一个td)之前插入它,而不是在最后插入它:

代码语言:javascript
复制
// Find the last element, and add the new cell before it.
$newRow.find("td:last").before($newCell);

小提琴在这里

如果希望更具体,可以通过选择器中的文本指定元素。因此,在本例中,可以在包含文本td'Abc'元素之前进行状态声明。

代码语言:javascript
复制
$newRow.find("td:contains('Abc')").before($newCell);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37683181

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档