2017-09-04 89 views
1

我在javascript上创建了一个过滤器搜索列表,并将列表中的元素嵌套在3个不同的类别下。CSS不识别悬停颜色集

  • CUENTA NT
  • 培训在工作中
  • 手册德procedimientos

表的伟大工程,同时寻找任何条款,并立即检索它们。但是,该列表的目的是增长很多,我希望表格在鼠标悬停结果时显示与标题上的颜色类似的颜色。

问题是这样的:

尽管background-color属性在CSS片被正确地限定,也看不出任何颜色,而将鼠标上的任何结果的显示。

CSS

#myUL李A.1:悬停:没有(.header){背景颜色:#FCF3CF;}

function myFunction() { 
 
    var input, filter, ul, li, a, i; 
 
    input = document.getElementById("myInput"); 
 
    filter = input.value.toUpperCase(); 
 
    ul = document.getElementById("myUL"); 
 
\t li = ul.getElementsByTagName("li"); 
 
\t 
 
\t 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 { 
 
\t background-color: #F9E79F; 
 
\t border: 1px solid #ddd; 
 
\t width:100%; 
 
\t height:50px; 
 
\t padding-left:10px; 
 
\t text-align:left; 
 
\t text-transform:uppercase; 
 
} 
 

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

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

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

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

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

 

 

 

 
p.invisible {visibility:hidden; 
 
\t \t \t display:inline; 
 
\t \t \t font-size:0.5px; 
 
\t \t \t text-align:center; 
 
\t \t \t }
<!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"> 
 
    
 
\t <table id="myTable1"> 
 
\t <tr><th class="com">Cuenta NT</th></tr> 
 
\t </table> 
 

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

 

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

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

 

 

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

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

 
</ul> 
 

 
</body> 
 
</html>

+0

我不明白究竟是什么的问题。添加一个关于如何处理你的代码片段(写什么)的完整说明,发生了什么,它是错误的,你想要得到什么... – Dekel

+0

我在这里建议反对表。虽然我喜欢这个概念,但是数据显示的表格并不适用于像这样的更复杂的数据集,其中包含多个标题。 A11y将很难读到这一点。请尝试使用适当标题的div? – Dominik

+0

它旨在成为人力资源部门的一张小桌子。没有什么奇特的,他们想要取代Excel电子表格 – T100

回答

2

首先,类名称不能以数字字符开头,所以我改名为你的类.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"); 
 
\t li = ul.getElementsByTagName("li"); 
 
\t 
 
\t 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 { 
 
\t background-color: #F9E79F; 
 
\t border: 1px solid #ddd; 
 
\t width:100%; 
 
\t height:50px; 
 
\t padding-left:10px; 
 
\t text-align:left; 
 
\t text-transform:uppercase; 
 
} 
 

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

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

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

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

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

 

 

 

 
p.invisible {visibility:hidden; 
 
\t \t \t display:inline; 
 
\t \t \t font-size:0.5px; 
 
\t \t \t text-align:center; 
 
\t \t \t }
<!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"> 
 
    
 
\t <table id="myTable1"> 
 
\t <tr><th class="com">Cuenta NT</th></tr> 
 
\t </table> 
 

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

 

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

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

 

 

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

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

 
</ul> 
 

 
</body> 
 
</html>

+0

嘿,谢谢你的建议。我知道用数字命名类有些问题,但我不知道是什么。 ü保存了我的$%$ bro, – T100

1

待办事项不使用数字作为html类的名称,它也会混淆“类”和CSS的含义。 我改变了1,2,3到child1,child2,child3 我改变了他们的CSS,!重要的是确保风格适用(所以它不会被覆盖)。所有的

function myFunction() { 
 
    var input, filter, ul, li, a, i; 
 
    input = document.getElementById("myInput"); 
 
    filter = input.value.toUpperCase(); 
 
    ul = document.getElementById("myUL"); 
 
\t li = ul.getElementsByTagName("li"); 
 
\t 
 
\t 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.child1 > a:hover:not(.header) { 
 
    background-color: #FCF3CF !important; 
 
} 
 

 
#myUL li.child2 > a:hover:not(.header) { 
 
    background-color: #D5F5E3 !important; 
 
} 
 

 
#myUL li.child3 > a:hover:not(.header) { 
 
    background-color: #D6EAF8 !important; 
 
} 
 

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

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

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

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

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

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

 

 

 

 
p.invisible {visibility:hidden; 
 
\t \t \t display:inline; 
 
\t \t \t font-size:0.5px; 
 
\t \t \t text-align:center; 
 
\t \t \t }
<!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"> 
 
    
 
\t <table id="myTable1"> 
 
\t <tr><th class="com">Cuenta NT</th></tr> 
 
\t </table> 
 

 
\t <li class="child1"><a href="#">Cuenta NT 
 
\t <p class="invisible"> 
 
\t Cuenta NT 
 
\t </p></a></li> 
 

 

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

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

 

 

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

 
\t <li class="child3"><a href="#">Manual de procedimientos 
 
\t <p class="invisible"> 
 
\t Manual de procedimientos 
 
\t </p></a></li> 
 

 
</ul> 
 

 
</body> 
 
</html>

+0

嘿马里奥。谢谢你的回答。我刚刚注意到你先回答了我的问题,所以我想我也应该支持你。我在stackoverflow中还不是很有经验。有什么方法可以帮助您在网站上声名卓着?让我知道,谢谢。 – T100

+0

@ T100别担心,我很高兴我们帮助你! – MarioE

+0

你当然做到了。有一个好男人! – T100