您可以在表格数据中读入二维数组,然后用转置的矩阵覆盖表格数据。
function reArrange() {
var table = $('table#theTable');
// we will store all the table-data in a two-dim array:
var data = new Array();
// find all rows (row-index: i):
$(table).find('tr').each(function(i, row) {
// find all cells in this row (col-index: j):
$(row).find('td').each(function(j, cell) {
// make sure, we got the array right:
if (data[j] === undefined) {
data[j] = new Array();
}
// use col-index as first index in the array:
data[j][i] = $(cell).html();
});
});
// reset table:
$(table).find('tr').remove();
// re-fill table
$(data).each(function(i, elem){
var row = $('<tr/>');
$(elem).each(function(j, col) {
cell = $('<td/>').html(col);
$(row).append(cell);
});
$(table).append(row);
});
}
reArrange();
看一看这个的jsfiddle:http://jsfiddle.net/66YpC/2/
我希望,这就是你要找的人。如果您需要更多关于代码的信息,请告诉我!
这移动到3列,但如果我需要它在2列? – user2036094 2013-02-10 19:33:41
应该适用于任意数量的列和行。试一试。只需修改jsFiddle中的HTML标记,然后单击运行。 – ditscheri 2013-02-12 08:37:19