我有这个表,我试图根据单个id显示两行,但只有col-1和col-2根据下拉选择显示。 col-3& col-4永远不会消失。我知道在一个文档中多次使用唯一ID并不是最好的做法,这就是为什么我拥有所有的" td"在一个" tr"有一个独特的ID,我正在努力弄清楚为什么" inv" class仅适用于第一行或前两行" td"
有什么线索?
<style>
.inv {
display: none;
}
</style>
<table class="blueTable">
<select id="target">
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
</select>
<tbody>
<tr id="CA" class="inv">
<td>
col-1
</td>
<td>
Col-2
</td>
<td>
col-3
</td>
<td>
col-4
</td>
</tr>
</tbody>
</table>
<script>
document
.getElementById('target')
.addEventListener('change', function () {
'use strict';
var vis = document.querySelector('.vis'),
target = document.getElementById(this.value); // get the option id selected
if (vis !== null) { // reset the vis tag state
vis.className = 'inv';
}
if (target !== null ) {// if target not null then set the target link to visable state
target.className = 'vis';
}
});
</script>
这是css代码
table.blueTable {
font-family: Georgia, serif;
border: 1px solid #1C6EA4;
background-color: #EEEEEE;
width: 100%;
text-align: left;
border-collapse: collapse;
}
table.blueTable td, table.blueTable th {
border: 1px solid #CFCFCF;
padding: 3px 2px;
}
table.blueTable tbody td {
font-size: 12px;
}
table.blueTable tr:nth-child(even) {
background: #D0E4F5;
}
table.blueTable thead {
background: #1C6EA4;
background: -moz-linear-gradient(top, #5592bb 0%, #327cad 66%, #1C6EA4 100%);
background: -webkit-linear-gradient(top, #5592bb 0%, #327cad 66%, #1C6EA4 100%);
background: linear-gradient(to bottom, #5592bb 0%, #327cad 66%, #1C6EA4 100%);
border-bottom: 2px solid #9C9C9C;
}
table.blueTable thead th {
font-size: 15px;
font-weight: bold;
color: #FFFFFF;
border-left: 2px solid #D0E4F5;
}
table.blueTable thead th:first-child {
border-left: none;
}
table.blueTable tfoot td {
font-size: 14px;
}
table.blueTable tfoot .links {
text-align: right;
}
table.blueTable tfoot .links a{
display: inline-block;
background: #1C6EA4;
color: #FFFFFF;
padding: 2px 8px;
border-radius: 5px;
}
答案 0 :(得分:0)
W3Schools上的网络技术有一些优点。因此,您的JavaScript需要进行一些更改。我不确定你到底想要做什么。但是如果你添加以下代码
改变你的JS:
document.getElementById('target').addEventListener('change', function () {
target = this; // get the option id selected
if (target !== null ) {
//depending on opyion selected, use its value to get the
//table/tr you want to display
document.getElementById(this.value).className = "vis";
}});
在CSS中添加:
.vis{
display: block;
}
还要看看这个正在运行的JSFiddle:fiddle