【发布时间】:2021-12-05 22:26:22
【问题描述】:
我正在尝试制作嵌套的手风琴(你展开一个,然后在里面你可以展开另一个),但由于某种原因,里面的手风琴不起作用。
这是基于 w3 school 的手风琴代码,但我用图像替换了按钮。我的逻辑是,如果我复制粘贴第一个手风琴并将其重命名为“手风琴 1”,我可以稍后调用手风琴 1,并且会发生与原始手风琴相同的事情。
但是当我单击内部手风琴按钮时,没有任何反应。我不太确定为什么会这样,因为它似乎是直接复制粘贴。我不确定这里是否需要做一些不同的事情
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight) {
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
});
}
var acc1 = document.getElementsByClassName("accordion1");
var i;
for (i = 0; i < acc1.length; i++) {
acc1[i].addEventListener("click", function() {
this.classList.toggle("active");
var panel = this.nextElementSibling;
if (panel.style.maxHeight) {
panel.style.maxHeight = null;
} else {
panel.style.maxHeight = panel.scrollHeight + "px";
}
});
}
.accordion {
background-image: url(https://i.imgur.com/Jqsaukf.png);
background-repeat: no-repeat;
background-position: 50% 50%;
/* put the height and width of your image here */
height: 50px;
width: 200px;
border: none;
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
transition: 0.4s;
}
.active,
.accordion:hover {
background-color: #ccc;
}
.accordion:after {
content: '\002B';
color: #777;
font-weight: bold;
float: right;
margin-left: 5px;
}
.active:after {
content: "\2212";
}
.accordion1 {
background-image: url(https://i.imgur.com/VXlZ0Ja.png);
background-repeat: no-repeat;
background-position: 50% 50%;
/* put the height and width of your image here */
height: 500px;
width: 200px;
border: none;
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
transition: 0.4s;
}
.active1,
.accordion1:hover {
background-color: #ccc;
}
.accordion1:after {
content: '\002B';
color: #777;
font-weight: bold;
float: right;
margin-left: 5px;
}
.active1:after {
content: "\2212";
}
.panel {
padding: 0 18px;
background-color: white;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
}
<h2>Accordion with symbols</h2>
<p>In this example we have added a "plus" sign to each button. When the user clicks on the button, the "plus" sign is replaced with a "minus" sign.</p>
<button class="accordion"></button>
<div class="panel">
<button class="accordion1"></button>
<div class="panel1">
<p>Lorem ipsum dolor sit amet</p>
</div>
</div>
<br>
<button class="accordion"></button>
<div class="panel">
<p><img src="https://i.imgur.com/Jqsaukf.png" alt="Italian Trulli"></p>
</div>
<button class="accordion"></button>
<div class="panel">
<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>
编辑:我改变了我混淆的变量名;即 panel 和 panel1 以及 active 和 active 1 并且它可以工作,但我仍然想知道是否有更简洁/有效的方法来做到这一点。
我想要有 10 多个带有不同图像作为按钮的手风琴,所以如果没有办法压缩它,这个模块最终会很疯狂。也许是一个替换手风琴数组中每个图像的 for 循环?不确定这是否有意义,甚至可以使用 html/javascript/css。
【问题讨论】:
标签: javascript html css accordion