gpt4 book ai didi

css - 将鼠标悬停在列上时如何使列突出显示?

转载 作者:行者123 更新时间:2023-11-28 13:04:30 24 4
gpt4 key购买 nike

本学期我正在学习网络入门类(class),我完成了所有必须做的事情,所以没有什么能阻止我学习我想使用的东西。我有一个表格,第一行是类别,接下来的几行是数字,设置得像危险游戏。当我翻过一列时,我希望整个列都突出显示,以此类推。我的问题是,当我将鼠标悬停在我的列上时,它会突出显示该列,但是当我向下查看该列中的每个特定单元格时,它们会逐渐取消突出显示。我只是想让他们保持突出显示!我从事 CSS 工作,以前从未使用过 Jquery,只使用过一点 Javascript,但我愿意为所有人接收信息。如果可能的话,最好是 CSS。

CSS

td:hover{
background-color: 5a5b93;
}

td {
position: relative;
}

td:hover::after {
background-color: #563A81;
content: '\00a0';
height: 10000px;
left: 0;
position:absolute;
width: 100%;
z-index: -1;
}

最佳答案

我看不出有什么方法可以用 CSS 来完成。这是一个fiddle使用 jQuery。

片段

var th = $("th, td");

th.each(function() {
var $this = $(this),
n = $this.index() + 1;
$this.hover(function() {
var col = $("tr > td:nth-child(" + n + ")");
col.addClass("hovered");
});
});

用法

jQuery 是一个 Javascript 库。这意味着它需要源代码。在执行 jQuery 脚本之前需要加载库本身,因为默认情况下浏览器不“知道”jQuery。您可以通过在文档中添加脚本来完成此操作。最好在您的样式表之后 ( *.css ) 和之前其他 <script> 的。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

现在已经解决了,我们可以将上面的代码添加到我们的文档中。不过要小心! JavaScript 只能在元素已经加载时调用它。那么,我们要做的是在加载整个文档(以及我们需要的元素)时执行我们的代码片段。我们可以通过将上述代码放入文档就绪函数来做到这一点:

$(document).ready(function () {
var th = $("th, td");

th.each(function () {
var $this = $(this),
n = $this.index() + 1;
$this.hover(function () {
var col = $("tr > td:nth-child(" + n + ")");
col.addClass("hovered");
});
});
});

请记住将此代码段放入 <script> -tags 和 after jQuery 库。

文档的“head”看起来像这样(按顺序):

<head>
<!-- Bunch of meta tags -->
<link href="/stylesheet.css" rel="stylesheet" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
var th = $("th, td");

th.each(function () {
var $this = $(this),
n = $this.index() + 1;
$this.hover(function () {
var col = $("tr > td:nth-child(" + n + ")");
col.addClass("hovered");
});
});
});
</script>
</head>

希望这对您有所帮助。

关于css - 将鼠标悬停在列上时如何使列突出显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15933907/

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