使用 jQuery 循环遍历单元格或行很简单,但循环遍历列的单元格并不简单。
//for cells of rows I will do this
$('table tr').each(function(index,elem)...//loop through cell of row [index]
有人建议一种循环遍历列单元格的简单方法吗?
Edit:我读错了原来的问题。这个例子 http://jsfiddle.net/TJRPL/2/将循环遍历表中的所有单元格,首先按单元格排序。
Markup:
<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());
});
Result:
a
b
c
d
e
f
g
h
i
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)