尝试仅使用HTML和CSS在表中实现可折叠/可扩展行



我遵循了一个教程,向你展示了如何使可扩展/可折叠的内容,我能够让它完美地工作;然而,我有一些麻烦适应它在一个表中工作,那里有可以扩展的隐藏行。这个想法是,你点击视频按钮,一个隐藏行展开,显示嵌入的视频。现在,即使复选框被选中,它也不会展开内容。下面是我的代码的简化版本:

<html>
<head>
<style>
th {padding: 5px;
border-bottom: 1px solid white; }
</style>

<style>
table {
width: 100%;
border-left: none; 
border-right: none; 
border-collapse: collapse; }
</style>
<style type="text/css">
.accordion > input[name="collapse"] {
display:none; }
</style>

<style type="text/css">
.accordion .content {
background: rgba(105, 45, 118, 0.9);
visibility:collapse; }
</style>

<style type="text/css">
.accordion label {
color: #fff;
cursor: pointer;
font-weight: normal;
padding: 5px;
width:auto; 
display:block; }
</style>

<style type="text/css">
.accordion label:hover,
.accordion label:focus {
background: #FFFFFF; 
color: #000000; }
</style>

<style type="text/css">
.accordion table tr > input[name="collapse"]:checked ~ .content {
visibility:visible; }
</style>
</head>
<body>
<section class="accordion">
<table style="text-align:center">
<tr style="background-color:rgba(105, 45, 118, 1)">
<th>Place</th>
<th>Player</th>
<th>Platform</th>
<th>Time</th>
<th>Video</th>
</tr>

<tr style="background-color:rgba(105, 45, 118, 1)">
<td>1</td>
<td>Me</td>
<td>EMU</td>
<td>8:50</td>
<td class="handle"><label for="handle1">Video</label></td>
</tr>

<tr>
<input type="checkbox" name="collapse" id="handle1"> 
<td colspan="5"  class="content">
<iframe width="560" height="315" src="https://www.youtube.com/embed/Yx21y2jcB9Q" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</td>
</tr>
</table>
</section>
</body>
</html>

我删除了多余的样式标签并分析了代码,我注意到仅在标签内的输入没有在html中呈现,因此必须在html和css

中进行一些更改。我添加并更改了一些东西,但现在它可以工作了

<html>
<head>
<style>
th {
padding: 5px;
border-bottom: 1px solid white; 
}
table {
width: 100%;
border-left: none; 
border-right: none; 
border-collapse: collapse; 
}

.accordion input[name="collapse"] {
display:none; 
}
.accordion .content {
background: rgba(105, 45, 118, 0.9);

}
.accordion .wrappervideo {
display: none; 
}
.accordion label {
color: #fff;
cursor: pointer;
font-weight: normal;
padding: 5px;
width:auto; 
display:block; 
}
.accordion label:hover,
.accordion label:focus {
background: #FFFFFF; 
color: #000000; 
}
.accordion input[name="collapse"]:checked ~ .wrappervideo {
display: initial; 
}
</style>
</head>
<body>
<section class="accordion">
<table style="text-align:center">
<tr style="background-color:rgba(105, 45, 118, 1)">
<th>Place</th>
<th>Player</th>
<th>Platform</th>
<th>Time</th>
<th>Video</th>
</tr>

<tr style="background-color:rgba(105, 45, 118, 1)">
<td>1</td>
<td>Me</td>
<td>EMU</td>
<td>8:50</td>
<td class="handle"><label for="handle1">Video</label></td>
</tr>

<tr>
<td colspan="5" class="content">
<input type="checkbox" name="collapse" id="handle1" />
<div class="wrappervideo">
<iframe width="560" height="315" src="https://www.youtube.com/embed/Yx21y2jcB9Q" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>

</td>
</tr>
</table>
</section>
</body>
</html>

最新更新