【发布时间】:2021-05-13 06:21:03
【问题描述】:
这个问题可能有不同的答案,但不是我能找到的一种解决方案。
我有一个包含几行和单元格的表格。我需要将 cell2 的位置与 cell3 交换并且不更改任何内容。它可以工作,直到交换单元格。我只是错过了一些东西。
提前谢谢你!
<table id="myTable">
<tr>
<td>1 APPLES</td>
<td>3 ORANGES</td>
<td>2 BANANAS</td>
<td>4 GRAPES</td>
</tr>
<tr>
<td>1 APPLES</td>
<td>3 ORANGES</td>
<td>2 BANANAS</td>
<td>4 GRAPES</td>
</tr>
<tr>
<td>1 APPLES</td>
<td>3 ORANGES</td>
<td>2 BANANAS</td>
<td>4 GRAPES</td>
</tr>
</table>
这是我的 jquery -- 尝试使用 .each()
$('table tbody tr').each(function () {
//make sure I found the rows
$(this).css('border', '2px solid yellowgreen');
//find the 2nd cell
$(this).find('td:nth-child(2)').each(function () {
//make it blue so I know the correct cell is found
$(this).css('background', 'orange');
//find the 2nd child in the current row
$(this).find('td:nth-child(2)').each(function () {
//change color to check correct cell
$(this).css('background','tan');
//move the 2nd cell after the 3rd cell -- swap places.
$(this).insertAfter('td:nth-child(2)');
});
});
});
【问题讨论】: