需要html帮助在一个唯一的选定ID下显示多行

问题描述 投票:0回答:1

我有这个表,我试图显示基于单个id的两行,但只有col-1和col-2显示基于下拉选择。 col-3&col-4永远不会消失。我知道在一个文档中多次使用唯一ID并不是最好的做法,这就是为什么我将所有“td”置于带有唯一ID的“tr”下,我正在努力弄清楚为什么只有“inv”类适用于第一行或前两个“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;
}
css html-table selection stylesheet tablerow
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.