开发者

highlight a column

I saw in one of the jQuery books where you can highlight a column that's being sorted.

$('th.sortable').click(function() {
    var $th = $(this);
    var column = $th.index();
    var 开发者_Python百科$table = $th.closest('table');
    var rows = $table.find('tr:not(:has(th))').get();

Q: How do I add the 'hightlight' class to every cell in the column that was clicked?


There is a nth-child selector which you can use in this case.

$('th.sortable').click(function() {
    var $th = $(this),
        column = $th.index(),
        $table = $th.closest('table');

    $table.find('tr td:nth-child(' + (column+1) + ')').addClass('highlight');
});


I'd think you'd be able to do something like this:

Example: http://jsfiddle.net/4Sg8E/

$('th.sortable').click(function() {
    var $th = $(this);
    $th.closest('table').find('td:nth-child(' + ($th.index() + 1) + ')')
        .css('background','yellow');
});

It will get all <td> elements that are the same position as the <th> that was clicked, and hilight them.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜