jQuery仅在表格中突出显示所选列

Phi*_*ord 6 css jquery highlighting selected

我看到这篇文章突出显示偶数列,但是我可以只突出显示所选列吗?

这是他们使用的代码:

$("table.Table22 > tbody > tr > td:nth-child(even)").css("background","blue");
Run Code Online (Sandbox Code Playgroud)

但我想:注意:class="highlight"将在选定的列上,因此如果我选择了第3列,则将class="highlight"从第2列中删除并添加到第3列.jQuery需要根据所选列添加类.

<table class="tbl">
    <tr>
        <th class="firstColumn">
            Cell 1:Heading
        </th>
        <th class="highlight">
            Selected column so this should be highlighted
        </th>
        <th>
            Cell 3:Heading
        </th>
        <th>
            Cell 4:Heading
        </th>
        <th>
            Cell 5:Heading
        </th>
    </tr>
    <tr>
        <td>
            Cell 1:Row 1
        </td>
        <td class="highlight">
            Selected column so this should be highlighted
        </td>
        <td>
            Cell 3:Row 1
        </td>
        <td>
            Cell 4:Row 1
        </td>
        <td>
            Cell 5:Row 1
        </td>
    </tr>
    <tr>
        <td>
            Cell 1:Row 2
        </td>
        <td class="highlight">
            Selected column so this should be highlighted
        </td>
        <td>
            Cell 3:Row 2
        </td>
        <td>
            Cell 4:Row 2
        </td>
        <td>
            Cell 5:Row 2
        </td>
    </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

Rus*_*Cam 17

您可能需要查看jQuery tableHover插件来实现此目的.然后使用这样的东西

$('table.tbl').tableHover({
    colClass: 'hover', 
    clickClass: 'click', 
    headCols: true, 
    footCols: true 
}); 
Run Code Online (Sandbox Code Playgroud)

编辑:

像这样的东西?

工作演示 - 单击任何单元格以突出显示该列

演示代码 -

$(function() {
  var rows = $('table.tbl tr');  

  rows.children().click(function() {

    rows.children().removeClass('highlight');  
    var index = $(this).prevAll().length;  
    rows.find(':nth-child(' + (index + 1) + ')').addClass('highlight');

  });
});
Run Code Online (Sandbox Code Playgroud)