简体   繁体   English

如何在悬停时更改td的td背景颜色

[英]How to change td background-color of td on hover

I have a table in my project. 我的项目中有一张桌子。 Each td of this table has initial background-color:#fafafa . 该表的每个td都具有初始background-color:#fafafa

Here is my code: 这是我的代码:

<style>
    .uTbId tr td:hover {
        background-color: #0000CD;
        color: green;
    }
</style>
<table class="uTbId" border="1" cellspacing="0" style="width:100%;margin-top:30px" bordercolor="lightgray">
    <tr>
        <td style="position:absolute;width:100px;background-color:#fafafa">
            <input type="checkbox" id='Tom' />Tom</td>
    </tr>
</table>

And I want to change td backgroud-color from #fafafa to "#0000CD" when mouse over it, and I tried: 我想将鼠标悬停在#fafafa上的td背景颜色改为“#0000CD”,我试过:

<style>
.uTbId tr td:hover{background-color:#0000CD;color:green;}
</style>

But it works fail. 但它失败了。 The td backgroud-color is still #fafafa when mouse over the td. 当鼠标悬停在td上时, td背景颜色仍然是#fafafa。

And then I tried another way and I found that if td without initial backgroud-color, it works OK. 然后我尝试了另一种方式,我发现如果td没有初始背景色,它可以正常工作。 Like: 喜欢:

<td style="position:absolute;width:100px"><input type="checkbox" id='Tom'/>Tom</td> // works OK

I am confused, it seems nothing is wrong. 我很困惑,似乎没有错。 I can not delete the td initial background-color in my project. 我无法删除项目中的td初始背景颜色。

Anyone can help me? 有人可以帮帮我吗?

Set the default background-color in the css itself, otherwise inline styles can only be overriden by !important . 在css本身中设置默认的background-color ,否则内联样式只能被!important覆盖

.uTbId tr td {
  background-color: #fafafa;
  color: green;
}

Demo 演示

 .uTbId tr td { background-color: #fafafa; color: green; } .uTbId tr td:hover { background-color: #0000CD; color: green; } 
 <table class="uTbId" border="1" cellspacing="0" style="width:100%;margin-top:30px" bordercolor="lightgray"> <tr> <td style="width:100px;"><input type="checkbox" id='Tom' />Tom</td> </tr> </table> 

Here is the code you can check where on mouse hover on any td will give you, your given background-color. 下面是代码,您可以检查鼠标悬停在任何td上的位置,给定您的背景颜色。

 <table class="uTbId" border="1" cellspacing="0" style="width:100%;margin-top:30px" bordercolor="lightgray"> <tr> <td style="width:100px;"><input type="checkbox" id='Tom' />Tom</td> </tr> </table> <style> table.uTbId tr>td:hover{ background-color:#0000CD; } </style> 

CSS Specificity is preventing that, since style tag is more specific than given css style. CSS特性正在阻止这种情况,因为样式标记比给定的css样式更具体。 Answers here suggest to !important , but I urge you to not use it, unless it is absolutely necessary. 这里的答案建议!important ,但我敦促你不要使用它,除非绝对必要。 Just give your td a class and you will be done 只要给你的td上课,你就会完成

<style>
  .uTbId td.check {
    position: absolute;
    width: 100px;
    background-color: #fafafa
  }

  .uTbId td.check:hover {
        background-color: #0000CD;
        color: green;
  }

</style>
<table class="uTbId" border="1" cellspacing="0" style="width:100%;margin-top:30px" bordercolor="lightgray">
    <tr>
        <td class="check">
            <input type="checkbox" id='Tom' />Tom</td>
    </tr>
</table>

Add !important to the background-color on hover. 在悬停时添加!重要的背景颜色。

background-color:#0000CD !important

Since you added the style as inline, the white background you gave has taken precedence and hence you get this error. 由于您将样式添加为内联,因此您提供的白色背景优先,因此您会收到此错误。 Adding !important fixes this... 添加!重要修复此...

Codepen link: https://codepen.io/anon/pen/rpQybJ Codepen链接: https ://codepen.io/anon/pen/rpQybJ

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

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