【发布时间】:2019-09-12 00:01:31
【问题描述】:
我有一个表格,当单元格悬停时突出显示行和列。
这是实际操作中的一个小技巧: https://jsfiddle.net/cbsuwz5y/4/
控制dom的jquery在这里:
$("table").on('mouseover mousedown mouseleave', 'td', function (e) {
if (e.type == 'mouseover' || e.type == 'mousedown') {
$(this).parent().addClass("hover");
$(this).closest('table').find("col").eq($(this).index()).addClass("hover");
} else {
$(this).parent().removeClass("hover");
$(this).closest('table').find('col').eq($(this).index()).removeClass("hover");
}
});
我想要的是,当单元格悬停时,TH 元素会展开。我正在尝试通过应用这个 css 类来做到这一点:
.applied-css {
display: block;
position: absolute;
top: -60px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
width: 85px;
height: 85px;
background: white;
z-index: 1;
}
并使用这行 JS:
$(this).closest('table').find("col").eq($(this).index()).find(".div-default").addClass("applied-css");
谁能指出我正确的方向来让它工作?
我应该补充一点,当我说“扩展”TH 时,我基本上是在假装扩展的样子。我使用 CSS 在 TH 上覆盖一个大于 TH 正方形的正方形。这会稍微切断它旁边的单元格,但我可以接受。当我在 CSS 中使用 :hover 悬停实际 TH 时,我可以让它工作。但是当我将一个单元格悬停在表格上时,我想让它在 jquery 中工作以应用它。
【问题讨论】:
-
也许这里也包含相关的 HTML?
-
您的小提琴不会尝试应用
applied-css类。你能提供一个minimal reproducible example吗? -
您能具体说明您遇到问题的部分吗?鉴于您提供的内容,您似乎可以很容易地将
.applied-css应用到正确的位置。是css还是jquery? jsfiddle.net/ucpw0fxL -
使用 jquery(类似于上面),我在定位突出显示的行/列 TD 元素时遇到问题,然后将我的 CSS 应用于与其对应的 TH 元素。我会再做几个小提琴来展示我的尝试。
-
$(this).closest('table').find("tr:first").find("th").eq($(this).index())(见我之前的小提琴)
标签: jquery html css html-table