代码之家  ›  专栏  ›  技术社区  ›  Phillip Senn mpgn

突出显示列

  •  3
  • Phillip Senn mpgn  · 技术社区  · 15 年前

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

    Q: 如何将“hightlight”类添加到单击的列中的每个单元格?

    2 回复  |  直到 15 年前
        1
  •  5
  •   Harmen    15 年前

    有一个 nth-child 在这种情况下可以使用的选择器。

    $('th.sortable').click(function() {
        var $th = $(this),
            column = $th.index(),
            $table = $th.closest('table');
    
        $table.find('tr td:nth-child(' + (column+1) + ')').addClass('highlight');
    });
    
        2
  •  3
  •   user113716    15 年前

    例子: http://jsfiddle.net/4Sg8E/

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

    它会得到所有 <td> <th> 这是点击,并点燃他们。