简体   繁体   English

通过后缀将多个TD插入表中间

[英]Insert multiple TDs by Suffix into middle of Table

I have a table with 2-4 columns and several rows. 我有一张2-4列和几行的表格。 Some of the very right columns have a text, some have an image, some have both and some may have neither of them. 一些最右边的列有一个文本,一些有图像,一些有两个,有些可能都不是。

I want to copy the contents of the 2nd column into 3 newly created columns, but insert them in the middle (before text and the image, if applicable). 我想将第二列的内容复制到3个新创建的列中,但将它们插入中间(如果适用,在text和图像之前)。 So that it looks like that: 这样看起来像:

在此处输入图片说明

It is important, to loop over the suffixes as not all elements need to be moved. 重要的是,循环遍历后缀,因为并非所有元素都需要移动。

That approach: 该方法:

$("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('text')").index() > -1) {
    $newRow.find("td:contains('text')").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('text')").index() > -1) {
    $newRow.find("td:contains('text')").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);
  }
});

produces that result: 产生结果:

在此处输入图片说明

FIDDLE . FIDDLE

I've tried to update your fiddle with a different approach: https://jsfiddle.net/ed964u0o/1/ 我尝试用另一种方法来更新您的提琴: https : //jsfiddle.net/ed964u0o/1/

I saved the element to insert after in the object positions . 我保存了要在对象positions之后插入的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM