我正在尝试突出显示悬停状态下的表格单元格。使用jQuery或CSS很容易做到这一点,但我在单元格中有一些小div,当这些div悬停时,我希望div只突出显示而不是整个单元格;
因此,该图像显示了一个具有10个div的表格单元格;当其中一个小矩形div悬停时,需要突出显示它们,但当单元格中除div之外的任何部分悬停时,其本身都会突出显示。
发布于 2019-09-12 16:58:01
以下是使用jQuery实现这一点的一种方法:
$(function() {
var td = $("td");
// add/remove .hovering for table cells as normal
td.hover(function() {
$(this).addClass("hovering");
}, function() {
$(this).removeClass("hovering");
})
// add/remove .hovering for <div> and do opposite for parent <td>
$("div").hover(function() {
$(this).addClass("hovering");
td.removeClass("hovering");
}, function() {
$(this).removeClass("hovering").parents("td").addClass("hovering");
})
})div {
background-color: #fff;
border: 1px solid grey;
display: inline-block;
min-width: 50px;
min-height: 20px;
margin: 3px;
}
div.hovering {
background-color: lightskyblue;
}
table {
text-align: center;
width: 650px;
}
td {
border: 1px solid black;
font-size: 0;
padding: 5px;
}
td.hovering {
background-color: #ddd;
}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</td>
<td>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</td>
</tr>
</tbody>
</table>
https://stackoverflow.com/questions/57879491
复制相似问题