jQuery 仅在表中突出显示选定的列
我在突出显示偶数列上看到这篇文章,但我可以仅突出显示选定的列吗列?
这是他们使用的代码:
$("table.Table22 > tbody > tr > td:nth-child(even)").css("background","blue");
但我想:注意:class="highlight"
将位于所选列上,因此如果我选择第 3 列,class="highlight"< /code> 将从第 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>
I see this post on highlighting even columns but can I highlight only selected columns?
Here is the code they use:
$("table.Table22 > tbody > tr > td:nth-child(even)").css("background","blue");
But I would like: NOTE: the class="highlight"
will be on the selected columns, so if I selected column 3 the class="highlight"
would be removed from column 2 and added to column 3. jQuery needs to add the class based on selected column.
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可能想看看 jQuery tableHover 插件为了达成这个。 然后使用类似这样的东西
编辑:
这样的东西?
工作演示 - 单击任意单元格,突出显示该列演示
代码 -
You might want to take a look at jQuery tableHover plugin to achieve this. Then use something like this
EDIT:
Something like this?
Working Demo - Click on any cell, to highlight the column
Code from demo -
您是否考虑过使用 colgroups 而不是向每个单元格添加类?
我最近才开始亲眼目睹 colgroups 的力量,它们的工作原理如下:
这将渲染一个包含 5 列的表格,其中 1 列具有 css 类来突出显示第一列。 因此,实际上您唯一要做的就是向每个单元格的悬停添加一个函数,以便将突出显示类添加到相应的 colgroup 中。
您可以在这里找到完整的视频指南:表格修复标题,行+列突出显示。
*编辑答案,因为它不相关,我读错了问题,并回答了完全不同的问题。 (现在添加了正确的回复)
have you concidered using colgroups instead of adding classes to every cell?
i only recently started to see the power of colgroups myself, and they work like this:
this would render a table with 5 columns, where 1 column has css class to highlight the first col. so actually the only thing you have to do then, add a function to the hover of each cell, to just add the highlighting class to the corresponding colgroup.
there is a complete videoguide you can find right here:table fix header, and row + column highlighting.
*EDITED the answer because it was irrelevant, i read the question wrong, and answered to a totally different matter. (added a correct reply now)
以下是我为表格添加十字准线效果所使用的方法:
不过,在大型表格上运行似乎有点慢(突出显示滞后)。
Here's what I use for adding a cross-hair affect to my table:
Seems to run a bit slow on large tables though (the highlighting lags behind).
如果您在表标题中创建链接,则可以执行以下操作:
这会将单击的链接下方的所有单元格设置为“突出显示”类。
当然,您仍然应该在 CSS 文件中设置正确的样式:
If you create a link in your table headers, you can do something like this:
That will set all cells below the clicked link to class "highlight".
Of course, you should still set the correct style in your CSS file:
如果你想支持
colspan
和rowspan
,那么首先你需要建立表格单元格索引,即。 无论标记如何,都可识别每行中的单元格位置的矩阵。 然后,您需要跟踪所有感兴趣的表格单元格的事件,并计算它们在矩阵和共享垂直索引的列中的偏移量。结果查找如以下动画所示:
我编写了一个触发
wholly 的插件。列的 mouseenter
和wholly.mouseleave
事件。 完全。If you want to support
colspan
androwspan
, then first you need to build table cell index, ie. matrix that identifies cell positition in every row regardless of the markup. Then you need to track events of all the table cells of interest and calculate their offset in the matrix and the columns that share the vertical index.The resulting lookup is illustrated in the following animation:
I have written a plugin that triggers an
wholly.mouseenter
andwholly.mouseleave
events for columns. Wholly.您可以使用名为 Wholesale 的插件。 您可以在此处阅读有关如何集成它的完整教程
http://voidtricks.com/wholly-jquery-plugin/
You can use a plugin called wholly. You can read the full tutorial on how to integrate it here
http://voidtricks.com/wholly-jquery-plugin/