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

时间:2009-07-17 14:23:53

标签: jquery css highlighting selected

我在highlighting even columns上看到此帖子,但我是否可以仅突出显示所选列?

以下是他们使用的代码:

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

但我希望:注意: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>

6 个答案:

答案 0 :(得分:17)

您可能需要查看jQuery tableHover plugin才能实现此目的。然后使用类似的东西

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

修改

这样的东西?

Working Demo - 点击任意单元格,突出显示列

演示代码 -

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

  });
});

答案 1 :(得分:4)

您是否使用colgroups而不是为每个单元格添加类?

我最近才开始亲眼看到colgroups的力量,他们的工作方式如下:

.highlight {
    background-color: yellow; 
 }
     <table id="myTable">
    	   
    	       <colgroup class="highlight"></colgroup>
    	       <colgroup></colgroup>
    	       <colgroup></colgroup>
    	       <colgroup></colgroup>
    	       <colgroup></colgroup>
    	   
    	    <thead>
    	       <tr>
    	           <th>header1</th>
    	           <th>header2</th>
    	           <th>header3</th>
    	           <th>header4</th>
    	           <th>header5</th>
    	       </tr>
    	    </thead>
    	    <tbody>
    	       <tr>
    	           <td>cell a</td>
    	           <td>cell b</td>
    	           <td>cell c</td>
    	           <td>cell d</td>
    	           <td>cell e</td>
    	       </tr>
        	<tbody>
     <table>

这将呈现一个包含5列的表,其中1列具有css类以突出显示第一列。所以实际上你唯一需要做的就是在每个单元格的悬停中添加一个函数,只需将高亮类添加到相应的colgroup中。

您可以在此处找到完整的视频设备:table fix header, and row + column highlighting.

*编辑答案,因为它无关紧要,我读错了问题,回答了一个完全不同的问题。 (现在添加了正确的答复)

答案 2 :(得分:3)

这是我用来为我的桌子添加十字准线效果的内容:

$('tbody td').hover(function() {
    $(this).closest('tr').find('td,th').addClass('highlight');
    var col = $(this).index()+1;
    $(this).closest('table').find('tr :nth-child('+col+')').addClass('highlight');
}, function() {
    $(this).closest('tr').find('td,th').removeClass('highlight');
    var col = $(this).index()+1;
    $(this).closest('table').find('tr :nth-child('+col+')').removeClass('highlight');
});

虽然看起来在大型桌子上运行有点慢(突出显示滞后)。

答案 3 :(得分:1)

如果您在表格标题中创建了一个链接,则可以执行以下操作:

$("table.tbl th a").click(function() {
   var colnum = $(this).closest("th").prevAll("th").length;

   $(this).closest("table").find("tr td").removeClass("highlight");
   $(this).closest("table").find("tr td:eq(" + colnum + ")").addClass("highlight");
}

这会将点击链接下方的所有单元格设置为“突出显示”类。

当然,您仍应在CSS文件中设置正确的样式:

table.tbl tr .highlight {  background-color: blue; }

答案 4 :(得分:1)

如果您想支持colspanrowspan,那么首先需要构建表格单元索引,即。无论标记如何,都可以识别每一行中的单元格位置。然后,您需要跟踪所有感兴趣的表格单元格的事件,并计算它们在矩阵中的偏移量以及共享垂直索引的列。

结果查找在以下动画中说明:

wholly table index

我编写了一个插件,可以触发列的wholly.mouseenterwholly.mouseleave事件。 Wholly

答案 5 :(得分:0)

您可以使用名为fully的插件。您可以在此处阅读有关如何集成它的完整教程 http://voidtricks.com/wholly-jquery-plugin/