我有一个由服务器根据数据库中的数据动态创建的http表。服务器根据某些值设置表行style="background-color:#RRGGBB"
。此值来自数据库中的字符串,因此CSS不是一个选择。
一切都很好,但是覆盖了CSS,后者设置了将背景颜色悬停在表格行上的功能。
在这种情况下,如何在悬停时更改表行的颜色,同时又与数据库中的颜色保持某种相似性呢?
table tr:nth-child(2n):hover {
background-color: silver;
}
table tr:nth-child(2n+1):hover {
background-color: gray;
}
table tr:nth-child(2n) {
background-color: #DDDDEE;
}
table tr:nth-child(2n+1) {
background-color: #CCCCDD;
}
<table width="100%">
<tr>
<td>Line1</td>
</tr>
<tr>
<td>Line2</td>
</tr>
<tr>
<td>Line3</td>
</tr>
<tr style="background-color:#FFDDDD;">
<td>Line4 - With color, no hover effect</td>
</tr>
<tr style="background-color:#DDFFDD;">
<td>Line5 - With color, no hover effect</td>
</tr>
<tr>
<td>Line6</td>
</tr>
<tr>
<td>Line7</td>
</tr>
<tr>
<td>Line8</td>
</tr>
</table>
答案 0 :(得分:1)
您可以对悬停时的每个元素应用部分透明的盒子阴影。
table tr:hover {
box-shadow: inset 0 0 0 99999px rgba(0,0,0,0.2);;
}
table tr:nth-child(2n) {
background-color: #DDDDEE;
}
table tr:nth-child(2n+1) {
background-color: #CCCCDD;
}
<table width="100%">
<tr>
<td>Line1</td>
</tr>
<tr>
<td>Line2</td>
</tr>
<tr>
<td>Line3</td>
</tr>
<tr style="background-color:#FFDDDD;">
<td>Line4 - With color, no hover effect</td>
</tr>
<tr style="background-color:#DDFFDD;">
<td>Line5 - With color, no hover effect</td>
</tr>
<tr>
<td>Line6</td>
</tr>
<tr>
<td>Line7</td>
</tr>
<tr>
<td>Line8</td>
</tr>
</table>
答案 1 :(得分:0)
您可以使用onMouseOver和onMouseOut事件进行悬停。
onMouseOver="this.style.backgroundColor='grey'"
onMouseOut="this.style.backgroundColor='#FFDDDD'"
table tr:nth-child(2n):hover {
background-color: silver;
}
table tr:nth-child(2n+1):hover {
background-color: gray;
}
table tr:nth-child(2n) {
background-color: #DDDDEE;
}
table tr:nth-child(2n+1) {
background-color: #CCCCDD;
}
<table width="100%">
<tr>
<td>Line1</td>
</tr>
<tr>
<td>Line2</td>
</tr>
<tr>
<td>Line3</td>
</tr>
<tr style="background-color:#FFDDDD;" onMouseOver="this.style.backgroundColor='grey'" onMouseOut="this.style.backgroundColor='#FFDDDD'">
<td>Line4 - With color, no hover effect</td>
</tr>
<tr style="background-Color:#DDFFDD;" onMouseOver="this.style.backgroundColor='silver'" onMouseOut="this.style.backgroundColor='#DDFFDD'">
<td>Line5 - With color, no hover effect</td>
</tr>
<tr>
<td>Line6</td>
</tr>
<tr>
<td>Line7</td>
</tr>
<tr>
<td>Line8</td>
</tr>
</table>
答案 2 :(得分:0)
只需在悬停中添加td
table tr:nth-child(2n) td:hover {
background-color: silver;
}
table tr:nth-child(2n+1) td:hover {
background-color: gray;
}
table tr:nth-child(2n) {
background-color: #DDDDEE;
}
table tr:nth-child(2n+1) {
background-color: #CCCCDD;
}
<table width="100%">
<tr>
<td>Line1</td>
</tr>
<tr>
<td>Line2</td>
</tr>
<tr>
<td>Line3</td>
</tr>
<tr style="background-color:#FFDDDD;">
<td>Line4 - With color, no hover effect</td>
</tr>
<tr style="background-color:#DDFFDD;">
<td>Line5 - With color, no hover effect</td>
</tr>
<tr>
<td>Line6</td>
</tr>
<tr>
<td>Line7</td>
</tr>
<tr>
<td>Line8</td>
</tr>
</table>