【发布时间】:2021-02-22 14:12:59
【问题描述】:
我有一个带有多个可折叠按钮的 HTML 页面。我想在页面加载时展开一个按钮。现在默认情况下所有按钮都是折叠的。如何更改 HTML 或 CSS 以使特定按钮(当前周)展开而其余按钮折叠?以下是相关代码:
<!DOCTYPE html>
<html>
<head>
<title>
Prior_Recordings_HTML5
</title>
</head>
<link href="CSS/my_site.css" rel="stylesheet" type="text/css" />
<body>
<table class="table_for_collapse">
<tr>
<td>
<button class=collapsible>Current Week—November 8, 2020</button>
<div class=collapsible_content>
<a href="https://my_site_link_1" target="_blank" class="link3"
title="Sunday 11/8/2020">
Click here for Sunday, 11/8/2020<br />
</a>
<a href="https://my_site_link_2" target="_blank" class="link3"
title="Monday 11/9/2020">
Click here for 11/9/2020<br />
</a>
</div>
</td>
</tr>
<tr>
<td>
<button class=collapsible>Prior Week 1—November 1, 2020</button>
<div class=collapsible_content>
<a href="https://my_site_link_3" target="_blank" class="link3"
title="Sunday 11/1/2020">
Click here for Sunday, 11/1/2020<br />
</a>
<a href="https://my_site_link_4" target="_blank" class="link3"
title="Monday 11/2/2020">
Click here for 11/2/2020<br />
</a>
</div>
</td>
</tr>
</table>
<script>
var coll = document.getElementsByClassName("collapsible");
var i;
for (i = 0; i < coll.length; i++) {
coll[i].addEventListener("click", function() {
this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.maxHeight){
content.style.maxHeight = null;
} else {
content.style.maxHeight = content.scrollHeight + "px";
}
});
}
</script>
</body>
</html>
这是 CSS:
.collapsible {
background-color: #777;
color: white;
cursor: pointer;
font-size: 15px;
padding-top: 6px;
padding-bottom: 6px;
text-align: left;
width: 450px;
max-width: 1000px;
}
.active, .collapsible:hover {
background-color: #555;
}
.collapsible:after {
content: '\002B';
color: white;
font-weight: bold;
float: right;
margin-left: 5px;
}
.active:after {
content: "\2212";
}
.collapsible_content {
/*
padding: 0 18px;
*/
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
background-color: #EEE8AA;
}
感谢您查看此内容。
【问题讨论】:
标签: html button collapsable