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

有了这种蟑螂:
$("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的列之间,而不是在最右边。
小提琴。
发布于 2016-06-07 16:07:14
要在表的中间插入一些内容,您可以使用before()或after()。例如,我们希望在Abc项之前插入新的单元格,所以您可以使用下面的代码在Abc (最后一个td)之前插入它,而不是在最后插入它:
// Find the last element, and add the new cell before it.
$newRow.find("td:last").before($newCell);如果希望更具体,可以通过选择器中的文本指定元素。因此,在本例中,可以在包含文本td的'Abc'元素之前进行状态声明。
$newRow.find("td:contains('Abc')").before($newCell);https://stackoverflow.com/questions/37683181
复制相似问题