英文:
Pre-open nested collapsible with JavaScript
问题
我想要在有人访问页面时,所有按钮可折叠的部分都已经打开。他们应该能够随时单击任何按钮元素来打开/关闭可折叠部分。
以下是我目前正在使用的代码示例。当您单击关闭的父按钮时,子元素会自动打开。我需要稍微修改代码,以便所有按钮元素都以打开状态开始。
<button type="button" class="collapsible">Act 1</button>
<div class="content">
<button type="button" class="collapsible">Scene 1</button>
<div class="content">
<button type="button" class="collapsible">Snippet 1</button>
<div class="content">
<p>这里是示例文本。</p>
</div>
<button type="button" class="collapsible">Snippet 2</button>
<div class="content">
<p>这里是示例文本。</p>
</div>
<button type="button" class="collapsible">Snippet 3</button>
<div class="content">
<p>这里是示例文本。</p>
</div>
</div>
<button type="button" class="collapsible">Scene 2</button>
<div class="content">
<button type="button" class="collapsible">Snippet 1</button>
<div class="content">
<p>这里是示例文本。</p>
</div>
<button type="button" class="collapsible">Snippet 2</button>
<div class="content">
<p>这里是示例文本。</p>
</div>
<button type="button" class="collapsible">Snippet 3</button>
<div class="content">
<p>这里是示例文本。</p>
</div>
</div>
</div>
这段代码中的按钮元素已经默认为打开状态。
英文:
I'd like to have my button collapsibles all already open when someone visits the page. They should be able to click any of the button elements to open/close the collapsible at any time.
The following is an example of code I'm working with right now. The children element opens automatically when you click on a closed parent button. What I need is to modify the code slightly so that all the button elements start as open.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const collapsibles = document.querySelectorAll(".collapsible");
collapsibles.forEach(collapsible => {
collapsible.addEventListener("click", function() {
open(this);
const children = this.nextElementSibling.querySelectorAll('.collapsible');
children.forEach(open);
});
});
function open(element) {
element.classList.toggle("active");
var content = element.nextElementSibling;
if (content.style.display === "block") {
content.style.display = "none";
} else {
content.style.display = "block";
}
}
<!-- language: lang-css -->
.collapsible {
background-color: #0486C1;
color: white;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
}
.active,
.collapsible:hover {
background-color: #254062;
}
.collapsible::after {
content: '.collapsible {
background-color: #0486C1;
color: white;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
}
.active,
.collapsible:hover {
background-color: #254062;
}
.collapsible::after {
content: '\002B';
margin-left: 5px;
}
.collapsible.active::after {
content: "\2212";
}
.content {
padding: 0 18px;
display: none;
overflow: hidden;
background-color: #E2E2E2;
transition: max-height 0.2s ease-out;
}
p {
color: #000000;
}
2B';
margin-left: 5px;
}
.collapsible.active::after {
content: "12";
}
.content {
padding: 0 18px;
display: none;
overflow: hidden;
background-color: #E2E2E2;
transition: max-height 0.2s ease-out;
}
p {
color: #000000;
}
<!-- language: lang-html -->
<button type="button" class="collapsible">Act 1</button>
<div class="content">
<button type="button" class="collapsible">Scene 1</button>
<div class="content">
<button type="button" class="collapsible">Snippet 1</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>
<button type="button" class="collapsible">Snippet 2</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>
<button type="button" class="collapsible">Snippet 3</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>
</div>
<button type="button" class="collapsible">Scene 2</button>
<div class="content">
<button type="button" class="collapsible">Snippet 1</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>
<button type="button" class="collapsible">Snippet 2</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>
<button type="button" class="collapsible">Snippet 3</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>
</div>
</div>
<!-- end snippet -->
答案1
得分: 0
你可以简单地将类active
添加到所有可折叠元素的初始状态,或者你可以创建一个脚本来默认打开所有可折叠元素。
<button type="button" class="collapsible active">片段 1</button>
const collapsibles = document.querySelectorAll(".collapsible");
collapsibles.forEach(collapsible => {
collapsible.addEventListener("click", function() {
open(this);
const children = this.nextElementSibling.querySelectorAll('.collapsible');
children.forEach(open);
});
});
function open(element) {
element.classList.toggle("active");
}
document.querySelectorAll('.collapsible').forEach(open);
.collapsible {
background-color: #0486C1;
color: white;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
}
.active,
.collapsible:hover {
background-color: #254062;
}
.collapsible::after {
content: '.collapsible {
background-color: #0486C1;
color: white;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
}
.active,
.collapsible:hover {
background-color: #254062;
}
.collapsible::after {
content: '\002B';
margin-left: 5px;
}
.collapsible.active::after {
content: "-";
}
.collapsible + .content{
display:none;
}
.collapsible.active + .content{
display:block;
}
.content {
padding: 0 18px;
display: none;
overflow: hidden;
background-color: #E2E2E2;
transition: max-height 0.2s ease-out;
}
p {
color: #000000;
}
2B';
margin-left: 5px;
}
.collapsible.active::after {
content: "-";
}
.collapsible + .content{
display:none;
}
.collapsible.active + .content{
display:block;
}
.content {
padding: 0 18px;
display: none;
overflow: hidden;
background-color: #E2E2E2;
transition: max-height 0.2s ease-out;
}
p {
color: #000000;
}
<button type="button" class="collapsible">行动 1</button>
<div class="content">
<button type="button" class="collapsible">场景 1</button>
<div class="content">
<button type="button" class="collapsible">片段 1</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>
<button type="button" class="collapsible">片段 2</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>
<button type="button" class="collapsible">片段 3</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>
</div>
<button type="button" class="collapsible">场景 2</button>
<div class="content">
<button type="button" class="collapsible">片段 1</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>
<button type="button" class="collapsible">片段 2</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>
<button type="button" class="collapsible">片段 3</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>
</div>
</div>
英文:
You can simply add a class active
to all collapsibles initially
<button type="button" class="collapsible active">Snippet 1</button>
or you can make a script to open all collapsibles by default
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const collapsibles = document.querySelectorAll(".collapsible");
collapsibles.forEach(collapsible => {
collapsible.addEventListener("click", function() {
open(this);
const children = this.nextElementSibling.querySelectorAll('.collapsible');
children.forEach(open);
});
});
function open(element) {
element.classList.toggle("active");
}
// here
document.querySelectorAll('.collapsible').forEach(open)
<!-- language: lang-css -->
.collapsible {
background-color: #0486C1;
color: white;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
}
.active,
.collapsible:hover {
background-color: #254062;
}
.collapsible::after {
content: '\002B';
margin-left: 5px;
}
.collapsible.active::after {
content: "\2212";
}
.collapsible + .content{
display:none;
}
.collapsible.active + .content{
display:block;
}
.content {
padding: 0 18px;
display: none;
overflow: hidden;
background-color: #E2E2E2;
transition: max-height 0.2s ease-out;
}
p {
color: #000000;
}
<!-- language: lang-html -->
<button type="button" class="collapsible">Act 1</button>
<div class="content">
<button type="button" class="collapsible">Scene 1</button>
<div class="content">
<button type="button" class="collapsible">Snippet 1</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>
<button type="button" class="collapsible">Snippet 2</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>
<button type="button" class="collapsible">Snippet 3</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>
</div>
<button type="button" class="collapsible">Scene 2</button>
<div class="content">
<button type="button" class="collapsible">Snippet 1</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>
<button type="button" class="collapsible">Snippet 2</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>
<button type="button" class="collapsible">Snippet 3</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>
</div>
</div>
<!-- end snippet -->
Edit: I changed you css a little bit and it now uses the +
(sibling) selector of CSS to show all the .content
after an open collapsible
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论