开发者

Loop through the cells of a columns (not cells of a rows) with Jquery (or js) on a html tables?

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)...//开发者_StackOverflow社区loop through cell of row [index]

Any one suggest a simple method for looping through cells of a columns?


Edit: I misread the original question. This example will loop through all the cells in a table, ordered by their cells first.

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


$(".table_identifier tr > :nth-child(1)").each(function(index,elem).....

change 1 to whatever column you want to select

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜