2017-09-04 88 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