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

标签 css html-table selection stylesheet tablerow

我有这张表,我试图根据单个 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>

Here is the css code

 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;
}

最佳答案

W3Schools 上有一些不错的 Web 技术.因此,您的 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

关于css - 需要 html 帮助在一个唯一的选定 ID 下显示多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49016566/

相关文章:

css - 响应式 iframe,全宽,但设置高度

css - 如果表格太小,如何仅设置第一列来填充剩余空间或修剪其内容?

html - BootStrap 模态显示不正确

css - 如何解决 td 边框问题?

html - 将 td 一分为二

selection - qooxdoo - 自定义列表项小部件选择

html - 通过 GoDaddy,主页看起来有所不同

mysql - Rails - 如何遍历行值为 'x' 的数据库表列并显示在 html 表中

list - KDB:如何从列表中做出(尽可能)均匀分布的选择?

uitableview - Tableview 选择将我的按钮隐藏在 tableview 单元格中