gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 22:20:23 24 4
gpt4 key购买 nike

我的问题是我的表格中已经有一些“突出显示”(意味着它们有自己的背景颜色来突出显示它们)单元格,当我使用代码更改整个单元格的颜色时,这些单元格不会更改它们的背景颜色当鼠标悬停在它们上方时行。

将鼠标悬停在一行上只会更改未突出显示的单元格的背景颜色。

如何解决这个问题,让整行的背景颜色都改变?

我有这个 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>

最佳答案

您可以仅在 CSS 中实现此目的。您只需要使 :hover 规则比 td.highlight 规则更具体。试试这个:

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

Example fiddle

关于javascript - 悬停时更改表格行颜色(jQuery 或 CSS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26606657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com