[英]javascript to allow me to collapse accordion that is extended
I have a side navigation bar that consists of multiple accordions that when expanded shows more navigation buttons.我有一个由多个手风琴组成的侧面导航栏,展开时会显示更多导航按钮。 I have a bit of JavaScript that will only allow one of those accordions to be expanded at a time.
我有一些 JavaScript,它一次只允许扩展其中一个手风琴。 However if I click on one accordion to expand it, when I click the same accordion it remain open.
但是,如果我点击一个手风琴来展开它,当我点击同一个手风琴时,它会保持打开状态。
I would like to get it so that I can expand and collapse the accordion without having to expand another accordion.我想得到它,以便我可以扩展和折叠手风琴而不必扩展另一个手风琴。
JavaScript: JavaScript:
var acc = document.getElementsByClassName("nav-link-bottom-main");
var i;
var last;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function() {
if (last) {
last.nextElementSibling.classList.toggle("show");
}
this.nextElementSibling.classList.toggle("show");
last = this;
}
}
html: html:
<div>
<button class="nav-link-bottom-main toggle"><a href="#">Endpoint Section 1</a></button>
<div class="panel">
<button class="nav-link-bottom-sub toggle"><a class="GET-icon"><center>GET</center></a><a href="#">endpoint 1.1</a></button>
<button class="nav-link-bottom-sub toggle"><a class="GET-icon"><center>GET</center></a><a href="#">endpoint 1.2</a></button>
<button class="nav-link-bottom-sub toggle"><a class="GET-icon"><center>GET</center></a><a href="#">endpoint 1.3</a></button>
</div>
<button class="nav-link-bottom-main toggle"><a href="#">Endpoint Section 2</a></button>
<div class="panel">
<button class="nav-link-bottom-sub toggle"><a class="GET-icon"><center>GET</center></a><a href="#">endpoint 2.1</a></button>
<button class="nav-link-bottom-sub toggle"><a class="GET-icon"><center>GET</center></a><a href="#">endpoint 2.2</a></button>
<button class="nav-link-bottom-sub toggle"><a class="GET-icon"><center>GET</center></a><a href="#">endpoint 2.3</a></button>
<button class="nav-link-bottom-sub toggle"><a class="GET-icon"><center>GET</center></a><a href="#">endpoint 2.4</a></button>
</div>
It looks like whenever if (last)
condition is true and it's the same element as this
, you're toggling show
class twice on same element, so probably this is why it remains open (actually it closes and opens again at the same time).看起来只要
if (last)
条件为真并且它与this
元素相同,您就会在同一元素上两次切换show
类,所以可能这就是它保持打开状态的原因(实际上它同时关闭并再次打开) .
So try to check if you're not toggling same element like this:因此,请尝试检查您是否没有像这样切换相同的元素:
if (last && last != this) {
last.nextElementSibling.classList.toggle("show");
}
this.nextElementSibling.classList.toggle("show");
And I hope it helps.我希望它有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.