简体   繁体   English

在悬停时更改表格行颜色(jQuery或CSS)

[英]Change table row color on hover (jQuery or CSS)

My problems is that I already got some 'highlighted' (meaning that they've got their own background color to highlight them) cells in my table that won't change their background color when I use code to change color of entire row when mouse is hoovering over them. 我的问题是我已经有一些'突出显示'(意味着他们有自己的背景颜色突出显示)我的表格中的单元格,当我使用代码改变整行的颜色时,它不会改变它们的背景颜色正在他们上空徘徊。

Hoovering over a row only changes background color of cells that aren't highlighted. 悬停在一行上只会改变未突出显示的单元格的背景颜色。

How do I fix this so entire row changes background color? 如何解决这个问题,以便整行改变背景颜色?

I've got this HTML table: 我有这个HTML表格:

 $(window).load(function() { $('#infotable tr').hover(function() { $(this).addClass('hover'); }, function() { $(this).removeClass('hover'); }); }); 
 #infotable td { padding:0.7em; border:#969696 1px solid; } .highlight { background:#DAFFD6; } .hover { background:yellow; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <table> <thead> <tr> <th></th> <th>Column 1</th> <th>Column 2</th> <th>Column 3</th> </tr> </thead> <tbody id="infotable"> <tr> <td>Row #1</td> <td>889 kg</td> <td class="highlight">151 kg</td> <td>192 kg</td> </tr> <tr> <td>Row #2</td> <td>784 kg</td> <td>15 kg</td> <td class="highlight">64 kg</td> </tr> </tbody> </table> 

You can achieve this in CSS alone. 你可以在CSS中实现这一点。 You just need to make the :hover rule more specific than the td.highlight . 您只需要使:hover规则比td.highlight更具体。 Try this: 尝试这个:

#infotable tr:hover td,
#infotable tr:hover td.highlight
{
    background:yellow;
}

Example fiddle 示例小提琴

Add class hover to all td insted of tr by changing javascript only. 通过仅更改javascript,将类悬停添加到tr的所有td

  $('#infotable tr').hover(function()
  {
    $(this).find('td').addClass('hover');
  }, function()
  {
    $(this).find('td').removeClass('hover');
  });

FIDDLE 小提琴

Just inherit the style from hover check this Fiddle 只需从悬停检查这个小提琴继承风格

.hover, .hover .highlight
{
    background:yellow;
}

try this in css 在css中尝试这个

#infotable tr td:hover
{
    background:yellow;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM