gpt4 book ai didi

javascript - CSS 无法识别悬停颜色集

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

我在 javascript 上创建了一个过滤器搜索列表,并将我的列表元素嵌套在 3 个不同的类别下。

  • 北领地昆塔
  • 在职培训
  • 程序手册

在查找这些术语中的任何一个并立即检索它们时,该表非常有用。但是,该列表的目的是增加很多,我希望当鼠标悬停在结果上时,表格显示的颜色与标题上的颜色相似。

问题是这样的:

尽管在 CSS 表中正确定义了 background-color 属性,但将鼠标悬停在任何结果上时我看不到任何颜色显示。

CSS

#myUL li a.1:hover:not(.header) {background-color: #FCF3CF;}

function myFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
ul = document.getElementById("myUL");
li = ul.getElementsByTagName("li");

for (i = 0; i < li.length; i++) {
a = li[i].getElementsByTagName("a")[0];
if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";

}
}
}
* {
box-sizing: border-box;
}

#myInput {
background-image: url('/css/searchicon.png');
background-position: 10px 12px;
background-repeat: no-repeat;
width: 100%;
font-size: 16px;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}

#myUL {
list-style-type: none;
padding: 0;
margin: 0;
}

#myUL li a {
border: 1px solid #ddd;
margin-top: -1px; /* Prevent double borders */
padding: 8px;
text-decoration: none;
font-size: 18px;
color: black;
background-color:#f6f6f6;
display: block;
}

#myUL li a.1:hover:not(.header) {
background-color: #FCF3CF;
}

#myUL li a.2:hover:not(.header) {
background-color: #D5F5E3;
}

#myUL li a.3:hover:not(.header) {
background-color: #D6EAF8;
}

#myTable1 {
background-color: #F9E79F;
border: 1px solid #ddd;
width:100%;
height:50px;
padding-left:10px;
text-align:left;
text-transform:uppercase;
}

#myTable th.com {
background-color: #F9E79F;
}

#myTable2 {
background-color: #76D7C4;
border: 1px solid #ddd;
width:100%;
height:50px;
padding-left:10px;
text-align:left;
text-transform:uppercase;
}

#myTable th.toj {
background-color: #76D7C4;
}

#myTable3 {
background-color: #85C1E9;
border: 1px solid #ddd;
width:100%;
height:50px;
padding-left:10px;
text-align:left;
text-transform:uppercase;
}

#myTable3 th.doc {
background-color: #85C1E9;
}




p.invisible {visibility:hidden;
display:inline;
font-size:0.5px;
text-align:center;
}
<!DOCTYPE html>
<html>

<head>

<link href="CSS/style.css" rel="stylesheet" type="text/css" />

</head>

<body>

<h2>Matriz de Búsqueda Global</h2>

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Ingresa palabra a buscar" title="Teclea para localizar">


<ul id="myUL">

<table id="myTable1">
<tr><th class="com">Cuenta NT</th></tr>
</table>

<li class="1"><a href="#">Cuenta NT
<p class="invisible">
Cuenta NT
</p></a></li>




<table id="myTable2">
<tr><th class="toj">Training on the Job</th></tr>
</table>

<li class="2"><a href="#">Training on the Job
<p class="invisible">
Training on the Job
</p></a></li>




<table id="myTable3">
<tr><th class="doc">Manual de procedimientos</th></tr>
</table>

<li class="3"><a href="#">Manual de procedimientos
<p class="invisible">
Manual de procedimientos
</p></a></li>

</ul>

</body>
</html>

最佳答案

首先,类名不能以数字字符开头,所以我将你的类重命名为.x1.x2.x3。其次,在您的第一个悬停规则上使用此选择器 #myUL li.x1 a:hover,并在其他悬停规则上使用类似的选择器,如下所示。

function myFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
ul = document.getElementById("myUL");
li = ul.getElementsByTagName("li");

for (i = 0; i < li.length; i++) {
a = li[i].getElementsByTagName("a")[0];
if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";

}
}
}
* {
box-sizing: border-box;
}

#myInput {
background-image: url('/css/searchicon.png');
background-position: 10px 12px;
background-repeat: no-repeat;
width: 100%;
font-size: 16px;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}

#myUL {
list-style-type: none;
padding: 0;
margin: 0;
}

#myUL li a {
border: 1px solid #ddd;
margin-top: -1px; /* Prevent double borders */
padding: 8px;
text-decoration: none;
font-size: 18px;
color: black;
background-color:#f6f6f6;
display: block;
}

#myUL li.x1 a:hover {
background-color: #FCF3CF;
}

#myUL li.x2 a:hover {
background-color: #D5F5E3;
}

#myUL li.x3 a:hover {
background-color: #D6EAF8;
}

#myTable1 {
background-color: #F9E79F;
border: 1px solid #ddd;
width:100%;
height:50px;
padding-left:10px;
text-align:left;
text-transform:uppercase;
}

#myTable th.com {
background-color: #F9E79F;
}

#myTable2 {
background-color: #76D7C4;
border: 1px solid #ddd;
width:100%;
height:50px;
padding-left:10px;
text-align:left;
text-transform:uppercase;
}

#myTable th.toj {
background-color: #76D7C4;
}

#myTable3 {
background-color: #85C1E9;
border: 1px solid #ddd;
width:100%;
height:50px;
padding-left:10px;
text-align:left;
text-transform:uppercase;
}

#myTable3 th.doc {
background-color: #85C1E9;
}




p.invisible {visibility:hidden;
display:inline;
font-size:0.5px;
text-align:center;
}
<!DOCTYPE html>
<html>

<head>

<link href="CSS/style.css" rel="stylesheet" type="text/css" />

</head>

<body>

<h2>Matriz de Búsqueda Global</h2>

<input type="text" id="myInput" onkeyup="myFunction()" placeholder="Ingresa palabra a buscar" title="Teclea para localizar">


<ul id="myUL">

<table id="myTable1">
<tr><th class="com">Cuenta NT</th></tr>
</table>

<li class="x1"><a href="#">Cuenta NT
<p class="invisible">
Cuenta NT
</p></a></li>




<table id="myTable2">
<tr><th class="toj">Training on the Job</th></tr>
</table>

<li class="x2"><a href="#">Training on the Job
<p class="invisible">
Training on the Job
</p></a></li>




<table id="myTable3">
<tr><th class="doc">Manual de procedimientos</th></tr>
</table>

<li class="x3"><a href="#">Manual de procedimientos
<p class="invisible">
Manual de procedimientos
</p></a></li>

</ul>

</body>
</html>

关于javascript - CSS 无法识别悬停颜色集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46044540/

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