javascript - 我需要默认打开可折叠的 div
问题描述
我使用 w3school 可折叠,我需要让它默认打开,我有什么选择,我该怎么做?
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";
}
});
}
.collapsible {
background-color: #777;
color: white;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
}
.active, .collapsible:hover {
background-color: #555;
}
.content {
padding: 0 18px;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
background-color: #f1f1f1;
}
<button class="collapsible">Open Collapsible</button>
<div class="content">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
任何帮助都意义重大。我尝试添加display: block
内容,但没有帮助。
解决方案
只需将内容更改为:
.content {
padding: 0 18px;
display: block;
overflow: hidden;
background-color: #f1f1f1;
}
之前发生的事情是您检查按钮的点击并显示内容。现在它将默认显示内容。
推荐阅读
- django - 我的两个应用程序中的一个在 django 中不起作用
- python - 如何正确地将我的模块文件导入我的测试文件?
- javascript - javascript在没有字符串连接的情况下更改元素的位置
- r - 如何在R中的点图中散布点?
- r - R:将列分成两列,但保留其余列
- jboss - Swing Client EJB Wildfly - 服务器端的新用户但旧用户上下文
- python - Python版本未更新
- caching - 如何解决 cloudflare 没有在我的手机上显示我的网站
- python - 两个坐标列表的欧几里得距离矩阵
- database - 在将寄存器数据存储到 mongodb 数据库中时出错