本文介绍了在html表上使用Jquery(或js)循环遍历列的单元格(不是行的单元格)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用jQuery很容易遍历单元格或行,但循环遍历列的单元格并不简单。
With jQuery is simple to loop through cells or rows, but it is not simple to loop through the cells of a columns.
//for cells of rows I will do this
$('table tr').each(function(index,elem)...//loop through cell of row [index]
任何人建议a循环遍历列的单元格的简单方法?
Any one suggest a simple method for looping through cells of a columns?
推荐答案
编辑:我误读了原始问题。 将遍历表格中的所有单元格,并按其单元格排序。
I misread the original question. This example will loop through all the cells in a table, ordered by their cells first.
加价:
<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
这篇关于在html表上使用Jquery(或js)循环遍历列的单元格(不是行的单元格)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!