Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将列(包括AD和td)移动到新位置_Javascript_Jquery_Html - Fatal编程技术网

Javascript 将列(包括AD和td)移动到新位置

Javascript 将列(包括AD和td)移动到新位置,javascript,jquery,html,Javascript,Jquery,Html,我有一个这样的表,第3列是复选框的一列 header1 header2 header3 cell1 cell2 checkbox3 cell1 cell2 checkbox3 cell1 cell2 checkbox3 我想像这样把第3列移到第1列之后 header1 header3 header2 cell1 checkbox3 cell2 cell1 checkbox3

我有一个这样的表,第3列是复选框的一列

header1   header2     header3
 cell1     cell2     checkbox3
 cell1     cell2     checkbox3
 cell1     cell2     checkbox3
我想像这样把第3列移到第1列之后

header1     header3      header2
 cell1     checkbox3      cell2
 cell1     checkbox3      cell2
 cell1     checkbox3      cell2
这就是我迄今为止所尝试的:

$('thead tr:nth-child(2)').insertAfter('thead tr:nth-child(0)');
$('tbody tr:nth-child(2)').insertAfter('tbody tr:nth-child(0)');
表的代码:

<table>
  <thead>
   <tr>
    <th>header1</th>
    <th>header2</th>
    <th>header3</th>
   </tr>
  </thead>
  <tbody>
   <tr>
    <td>cell1</td>
    <td>cell2</td>
    <td><input type="checkbox" />checkbox3</td>
   </tr>
   <tr>
    <td>cell1</td>
    <td>cell2</td>
    <td><input type="checkbox" />checkbox3</td>
   </tr>
   <tr>
    <td>cell1</td>
    <td>cell2</td>
    <td><input type="checkbox" />checkbox3</td>
   </tr>
  </tbody>
</table>

校长1
校长2
校长3
第1单元
细胞2
复选框3
第1单元
细胞2
复选框3
第1单元
细胞2
复选框3

但是它不起作用,我能解决这个问题吗?

像这样的事情怎么样:

function swapColumns (table, sourceCol, destCol) {
    let rows = $("tr", table);  // get rows in table
    let cols;   // declare cols variable
    rows.each(function() {
        cols = $(this).children("th, td");  // cols include th and td
        cols.eq(sourceCol).detach().insertBefore(cols.eq(destCol)); // swap 'em
    });
}

let table = $("#tableSelector");
swapColumns(table);

您的代码也是为什么您甚至要在表行上尝试一些事情,如果您说要切换列…