【问题标题】:how do I use the CSS hover on only SOME cells in a table?如何仅在表格中的某些单元格上使用 CSS 悬停?
【发布时间】:2016-02-24 03:18:15
【问题描述】:

如何仅在表格中的某些单元格上使用 CSS 悬停?
我可以在我不想应用的那些上关闭它吗? 我正在使用这个:

td:hover {
    border-style:dotted;
    border-color:#F60;
    border-width:medium;
    border-left-style:dotted;
    border-right-style:dotted;

}

但我需要它只适用于某些单元格

【问题讨论】:

  • 这个“某些单元格”是否有模式或只是随机单元格?

标签: html css


【解决方案1】:

您可以创建一个类并将其仅应用于您想要样式的单元格,而不是为所有单元格指定样式。将你的 CSS 更新为:

.myclass:hover {
    border-style:dotted;
    border-color:#F60;
    border-width:medium;
    border-left-style:dotted;
    border-right-style:dotted;

}

然后你在 HTML 代码中做这样的事情:

<table>
  <tr>
    <td class="myclass">Cell 1 with special hoover</td>
    <td>Cell 2</td>     
    <td>Cell 3</td>
  </tr>
</table>

【讨论】:

    【解决方案2】:

    &lt;td&gt; 标记支持 HTML 中的全局属性,因此您只需在代码中为您希望悬停的每个表格单元格添加一个类。

    ...
    <td class="cell-hover">Table Cell Data</td>
    ...
    

    修改您的 CSS 选择器以包含仅选择具有“cell-hover”类的 td 单元格。

    td:hover {
       border-style:dotted;
       border-color:#F60;
       border-width:medium;
       border-left-style:dotted;
       border-right-style:dotted;
    }
    

    通过在 CSS 中使用 .,您可以只选择具有后续类名称的元素

    【讨论】:

    • @honeycomb13 如果它解决了您的问题,请接受答案,并可能也投赞成票。
    猜你喜欢
    • 2020-08-28
    • 2014-10-21
    • 2017-11-28
    • 2017-03-16
    • 2016-03-21
    • 2017-01-28
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    相关资源
    最近更新 更多