Jquery 突出显示一列

Jquery 突出显示一列,jquery,Jquery,我在jQuery的一本书中看到,可以突出显示正在排序的列 $('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(); 问:如何将“hightlight”类添加到单击的列中的每个单元格中?在本例中

我在jQuery的一本书中看到,可以突出显示正在排序的列

$('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();

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

在本例中,您可以使用一个
第n个子项
选择器

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

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

我想你可以这样做:

示例:


它将获得与单击的
位置相同的所有
元素,并将它们点亮。

您的意思是
列+1
,我想,正如您刚才告诉我的那样。
。index()
是零基的,而
第n个子项
是1基的。index=0->nth child(1)@Harman-有人告诉过你索引是零基的,是的,我把它倒过来了。已经更新了。
$('th.sortable').click(function() {
    var $th = $(this);
    $th.closest('table').find('td:nth-child(' + ($th.index() + 1) + ')')
        .css('background','yellow');
});