简体   繁体   English

jQuery-更改表格单元格的位置

[英]jQuery - change table cells position

how can I change via jquery the cells position from 1 2 to 2 1 ? 如何通过jquery将单元格位置从1 2更改为2 1

<table id='mytable'>
   <tr>
       <td>1</td>
       <td>2</td>
   </tr>
</table>
$('#mytable tr td:eq(0)').appendTo('#mytable tr');

JSFIDDLE

If you want to change all the second td to first position in your table, then you can use: 如果要将所有第二个td更改为表中的第一个位置,则可以使用:

$.each($('#mytable tr td:eq(1)'), function() {
     $(this).insertBefore($(this).prev());
})


Actually, above code will not work if your table have more than one <tr> element, if that is the case then you need to use .find() : 实际上,如果您的表中有多个<tr>元素,则上述代码将不起作用,如果是这种情况,则需要使用.find()

$('#mytable tr').find('td:eq(1)').each(function() {
    $(this).insertBefore($(this).prev());
});

Fiddle Demo 小提琴演示

References: .each() , .find() , .insertBefore() , .prev() 参考文献: 。每个() , .find() , .insertBefore() , .prev()

with append 带附加

http://jsfiddle.net/F7HmQ/1/ http://jsfiddle.net/F7HmQ/1/

$(function(){
   var td = $("td").first() ;
    $("tr").first().append(td);    
});

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

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