简体   繁体   English

表格样式 - td:hover 规则是否可以覆盖 tr:hover?

[英]table styling - is it possible for a td:hover rule to override a tr:hover?

I was wondering if it's possible to have a table styled with :hover background rules for both a tr and a td with certain class so that when the particular td is hovered on, only that element gets the background applied and not the tr it's within.我想知道是否有可能为具有特定类的 tr 和 td 设置一个带有:hover背景规则样式的表格,以便当悬停特定 td 时,只有该元素会应用背景,而不是它所在的 tr。

So for example:例如:

 tr:hover { background: blue; } td.something:hover { background: yellow; }
 <table> <tr> <td></td> <td></td> <td class="something"></td> </tr> <table>

Here I'd like the table to highlight the row if the first two td elements are hovered, but not on the third td with the something css class.在这里,如果前两个 td 元素悬停,我希望表格突出显示该行,而不是在带有something css 类的第三个 td 上。

I was wondering if the :not selector could help somehow, but I haven't come up with a way yet.我想知道:not选择器是否能以某种方式提供帮助,但我还没有想出办法。 Is this possible?这可能吗?

Thanks谢谢

I think this is what you want:我认为这就是你想要的:

 tr:hover { background: blue; } tr:hover td.something { background: #FFF; } tr:hover td.something:hover { background: yellow; }
 <table> <tr> <td>aaa</td> <td>aaa</td> <td class="something">dddd</td> </tr> <table>

Sure it possible.当然有可能。 Here's the fiddle这是小提琴

HTML HTML

<table>
  <tr>
    <td>
      first td
    </td>
    <td>
      second td
    </td>
    <td>
      third td
    </td>
  </tr>
</table>

CSS: CSS:

   td:hover{
      background-color:red;
    }
    tr:hover{
      background-color:yellow;
    }
    td{
      border:1px solid black;
      padding: 5px;
    }

You can except tags with your css class as following您可以使用 css 类除标签,如下所示

 table { border: 1px solid blue; } tr:hover td:not(.something) { background: blue; }
 <table> <tr> <td>Cell 1</td> <td>Cell 2</td> <td class="something">Cell 3</td> </tr> <table>

Or if you want to some cells with bg yellow and some with blue same time:或者,如果您想要一些带有 bg 黄色和一些带有蓝色同时的单元格:

 tr:hover td:not(.something) { background: blue; } tr:hover td:not(.anotherthing) { background: yellow; }
 <table> <tr> <td class="anotherthing">Cell 1</td> <td class="anotherthing">Cell 2</td> <td class="something">Cell 3</td> </tr> <table>

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

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