jQuery很容易循環遍歷單元格或行,但循環遍歷列的單元格並不簡單。在html表格上使用Jquery(或js)循環遍歷列的單元格(不是行的單元格)?
//for cells of rows I will do this
$('table tr').each(function(index,elem)...//loop through cell of row [index]
任何人都建議一個簡單的方法來循環通過列的單元格?
jQuery很容易循環遍歷單元格或行,但循環遍歷列的單元格並不簡單。在html表格上使用Jquery(或js)循環遍歷列的單元格(不是行的單元格)?
//for cells of rows I will do this
$('table tr').each(function(index,elem)...//loop through cell of row [index]
任何人都建議一個簡單的方法來循環通過列的單元格?
編輯任何列:我誤解了原來的問題。 This example將循環遍歷表中的所有單元格,首先按其單元格排序。
標記:
<table class='sortable'>
<tr>
<td>a</td>
<td>d</td>
<td>g</td>
</tr>
<tr>
<td>b</td>
<td>e</td>
<td>h</td>
</tr>
<tr>
<td>c</td>
<td>f</td>
<td>i</td>
</tr>
</table>
的jQuery:
var cells = $('table.sortable td').sort(function(a, b) {
//compare the cell index
var c0 = $(a).index();
var c1 = $(b).index();
if (c0 == c1)
{
//compare the row index if needed
var r0 = $(a).parent().index();
var r1 = $(b).parent().index();
return r0 - r1;
}
else
return c0 - c1;
});
//console.log(cells);
cells.each(function() {
console.log($(this).html());
});
結果:
a
b
c
d
e
f
g
h
i
$(".table_identifier tr > :nth-child(1)").each(function(index,elem).....
變化1至要選擇
注意,索引1至n NOT 0至n – rlemon
對不起,我的問題不是很清楚:我的意思是循環遍歷所有列的表的單元格。在你的情況下,公式(1)必須循環遍歷所有列 – albanx
更新我的答案,我認爲這可能有所幫助。 – wsanville