在html表上使用Jquery(或js)循环遍历列的单元格(而不是行的单元格)?
使用jQuery可以简单地在单元格或行中循环,但在列的单元格中循环并不简单在html表上使用Jquery(或js)循环遍历列的单元格(而不是行的单元格)?,jquery,loops,html-table,Jquery,Loops,Html Table,使用jQuery可以简单地在单元格或行中循环,但在列的单元格中循环并不简单 //for cells of rows I will do this $('table tr').each(function(index,elem)...//loop through cell of row [index] 有人建议一种简单的方法来循环一列的单元格吗 $(".table_identifier tr > :nth-child(1)").each(function(index,elem).....
//for cells of rows I will do this
$('table tr').each(function(index,elem)...//loop through cell of row [index]
有人建议一种简单的方法来循环一列的单元格吗
$(".table_identifier tr > :nth-child(1)").each(function(index,elem).....
将1更改为要选择的任何列编辑:我误读了原始问题。将循环遍历表中的所有单元格,首先按单元格排序 标记:
<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>
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
对不起,我的问题不是很清楚:我的意思是在所有列中循环表的单元格。在你的例子中,等式(1)必须是一个循环,通过所有列。我的答案是,我认为这可能会有所帮助。