css - 使用纯 CSS 切换表格单元格
问题描述
有没有办法使用 CSS 没有 JavaScript 来切换(显示/隐藏)表格列或行?
我已经完成了以下代码,它适用于 div 但不适用于列或行
<label for="trigger">Toggle box</label>
<input id="trigger" type="checkbox">
<div class="box">
Harry I've got Toggled
</div>
<table>
<thead>
<tr>
<th colspan="2">The table header</th>
</tr>
</thead>
<tbody>
<tr >
<td class="box"> I am a column1, I've got same class but sadly did not get toggled! do you know why?</td>
<td> I am a column 2 I don't have any class</td>
</tr>
</tbody>
</table>
<style>
.box {
display: none;
}
#trigger:checked + .box {
display: block;
}
table,
td {
border: 1px solid #333;
}
thead,
tfoot {
background-color: #333;
color: #fff;
}
</style>
如何切换表格的列或行?
解决方案
您的选择器不正确,应该是#trigger:checked ~table .box
首先寻找下一个兄弟姐妹,然后寻找该兄弟姐妹的孩子(如果它站在里面)。
.box {
display: none;
}
#trigger:checked ~ .box,
#trigger:checked ~table .box {
display: block;
}
table,
td {
border: 1px solid #333;
}
thead,
tfoot {
background-color: #333;
color: #fff;
}
<label for="trigger">Toggle box</label>
<input id="trigger" type="checkbox">
<div class="box">
Harry I've got Toggled
</div>
<table>
<thead>
<tr>
<th colspan="2">The table header</th>
</tr>
</thead>
<tbody>
<tr >
<td class="box"> I am a column1, I've got same class but sadly did not get toggled! do you know why?</td>
<td> I am a column 2 I don't have any class</td>
</tr>
</tbody>
</table>
推荐阅读
- docker - QueryTree 测试连接返回 404 not found
- python - 如何在python中将整数月转换为年
- java - Kie 服务器:如何部署具有依赖项的 kjar 容器
- electron - Electron 应用程序可以处理单独的窗口吗?
- vba - 在 Word VBA 中将 InlineShape 图片保存到文件
- php - 将文件内容添加到 laravel 中的模型属性
- python - 将 Keras CNN 模型(model.h5 或 model.json)集成到 C# 项目中
- javascript - 关于更新 Firestore 文档的值
- geogebra - GeoGebra 3D 中的旋转角度
- mysql - MySQL – 无法查看不同表中的数据,因为作业号有后缀