简体   繁体   English

使用 javascript function 交换 html 表中的 2 列

[英]swap 2 columns in a html table using a javascript function

I need to write a Javascript function to swap the second column with the third column, we will open the console and write your function then call it to test.我需要写一个 Javascript function 将第二列与第三列交换,我们将打开控制台并编写您的 function 然后调用它进行测试。

调用函数后,第三列 (a3-b3-c3) 需要代替 (a2-b2-c2)

index.html索引.html

    <table style="margin:auto;" id="tabel1" class="mytabel" width="300" height="400" cellspacing="0" cellpadding="0"
        border="1">
        <tbody>
            <tr>
                <td> A1 </td>
                <td> A2 </td>
                <td> A3 </td>
            </tr>
            <tr>
                <td> B1 </td>
                <td> B2 </td>
                <td> B3 </td>
            </tr>
            <tr>
                <td> C1 </td>
                <td> C2 </td>
                <td> C3 </td>
            </tr>
        </tbody>
    </table>

script.js ( so far ) script.js(到目前为止)

function swap(){
    //Write your code here

    var tRows = document.getElementById('tabel1').children[0].children;

    var column2 = [];
    var column3 = [];


    for(var i = 0; i < tRows.length; i++) {
        column2[i] = tRows[i].children[1];
        column3[i] = tRows[i].children[2];
    }

    console.log("cell2 " , column2);
    console.log("cell3 " , column3);

}

swap();

Try this alone (no need for a function around it - just put this directly in the browser console):单独尝试(无需在其周围使用 function - 只需将其直接放在浏览器控制台中即可):

document.querySelectorAll('#tabel1 tr').forEach(tr=>tr.append(tr.querySelectorAll('td')[1]))

This works by, for each row of the table, appending the second (index 1) td to the end of the row.对于表的每一行,这可以通过将第二个(索引 1)td 附加到行的末尾来实现。 When you append an element somewhere, it is removed from where it was.当您 append 某处的元素时,它会从原来的位置移除。

 function swap(){ document.querySelectorAll('#tabel1 tr').forEach(tr=>tr.append(tr.querySelectorAll('td')[1])) } swap();
 <table style="margin:auto;" id="tabel1" class="mytabel" width="300" height="400" cellspacing="0" cellpadding="0" border="1"> <tbody> <tr> <td> A1 </td> <td> A2 </td> <td> A3 </td> </tr> <tr> <td> B1 </td> <td> B2 </td> <td> B3 </td> </tr> <tr> <td> C1 </td> <td> C2 </td> <td> C3 </td> </tr> </tbody> </table>

You can loop over all the rows and use .insertBefore to switch their positions.您可以遍历所有行并使用.insertBefore来切换它们的位置。

function swap(){
    const rows = document.querySelectorAll("#tabel1 > tbody > tr");
    for(const row of rows){
      row.insertBefore(row.children[2], row.children[1]);
    }
}

Demo:演示:

 function swap(){ const rows = document.querySelectorAll("#tabel1 > tbody > tr"); for(const row of rows){ row.insertBefore(row.children[2], row.children[1]); } } swap();
 <table style="margin:auto;" id="tabel1" class="mytabel" width="300" height="400" cellspacing="0" cellpadding="0" border="1"> <tbody> <tr> <td> A1 </td> <td> A2 </td> <td> A3 </td> </tr> <tr> <td> B1 </td> <td> B2 </td> <td> B3 </td> </tr> <tr> <td> C1 </td> <td> C2 </td> <td> C3 </td> </tr> </tbody> </table>

You need to be mindful that tables can have header and footer sections, as well as multiple tbodys.您需要注意表格可以有 header 和页脚部分,以及多个 tbody。

Using children[0].children is a little too abstract, there are historic rows and cells collections that are easier to use and more semantic than selectors.使用children[0] .children 有点太抽象了,有历史单元格collections 比选择器更容易使用和语义化。

In the case of a single tbody, you can get the table's rows , then adjust each row's cells , eg:在单个 tbody 的情况下,您可以获取表格的rows ,然后调整每一行的cells ,例如:

 function swapCells() { Array.from(document.querySelector('#tabel1').rows).forEach(row => row.append(row.cells[1])); }
 <table style="margin:auto;" id="tabel1" class="mytabel" width="300" height="100" cellspacing="0" cellpadding="0" border="1"> <tbody> <tr><td> A1 </td><td> A2 </td><td> A3 </td></tr> <tr><td> B1 </td><td> B2 </td><td> B3 </td></tr> <tr><td> C1 </td><td> C2 </td><td> C3 </td></tr> </tbody> </table> <button onclick="swapCells()">Swap rows</button>

PS The misspelling "tabel" is an additional challenge. PS 拼写错误的“标签”是一个额外的挑战。 :-) :-)

You could use childNodes instead of children the following code will solve the issue:您可以使用childNodes而不是children ,以下代码将解决该问题:

 table.insertBefore(table.childNodes[1].childNodes[3], table.childNodes[2].childNodes[5])

Where table is the Table element, => childNodes[1] is the <tbody> (because of how nodes work. the 0th element is actually empty text) => childNodes[3] and [5]are the <tr> s (counting in the text nodes)其中 table 是 Table 元素,=> childNodes[1] 是<tbody> (因为节点的工作方式。第 0 个元素实际上是空文本)=> childNodes[3] 和 [5] 是<tr> s (计算文本节点)

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

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