我有几个手风琴下拉菜单,可以分别打开和关闭它们,但是我真正想要实现的是
这就是我能够实现的目标
<div class="a0f13_2aGAs">
<div class="_5cb4a_2yTAQ"><span>Categories</span></div>
<ul class="_44ec6_3AFlz" id="accordion">
<li class="_99e51_2Fldz">
<button class="accordion"><a href="#y">Main drop 1</a></button>
<div class="panel">
<ul class="cat">
<li class="cat">
<div>
<a class="cat" href="#x">
<span class="_33353_3KdIO">
PC</span><span class="_3b1e8_3gyVo">
<svg height="16" viewBox="0 0 9 16" width="9"
xmlns="http://www.w3.org/2000/svg" class=""
name="chevron-next-outline">
<path
d="M.659.65a.532.532 0 0 0 0 .75l6.583 6.593-6.583 6.606a.532.532 0 0 0 0 .75.527.527 0 0 0 .747 0L8.349 8.38a.519.519 0 0 0 .155-.375.54.54 0 0 0-.155-.375L1.406.662A.516.516 0 0 0 .659.65z"
fill="#50545B" fill-rule="evenodd"></path>
</svg>
</span>
</a>
</div>
</li>
<li class="cat">
<div>
<a class="cat" href="#x">
<span class="_33353_3KdIO">
PC 2</span><span class="_3b1e8_3gyVo">
<svg height="16" viewBox="0 0 9 16" width="9"
xmlns="http://www.w3.org/2000/svg" class=""
name="chevron-next-outline">
<path
d="M.659.65a.532.532 0 0 0 0 .75l6.583 6.593-6.583 6.606a.532.532 0 0 0 0 .75.527.527 0 0 0 .747 0L8.349 8.38a.519.519 0 0 0 .155-.375.54.54 0 0 0-.155-.375L1.406.662A.516.516 0 0 0 .659.65z"
fill="#50545B" fill-rule="evenodd"></path>
</svg>
</span>
</a>
</div>
</li>
</ul>
</div>
</li>
<li class="_99e51_2Fldz">
<button class="accordion"><a href="#y">Main drop 2</a></button>
<div class="panel">
<ul class="cat">
<li class="cat">
<button class="accordion ac2" data-parent="#myGroup"><a href="#y">sub drop 1</a></button>
<div class="panel">
<ul class="cat">
<li class="cat">
<div>
<a class="cat" href="#x">
<span class="_33353_3KdIO">
PC</span><span class="_3b1e8_3gyVo">
<svg height="16" viewBox="0 0 9 16" width="9"
xmlns="http://www.w3.org/2000/svg" class=""
name="chevron-next-outline">
<path
d="M.659.65a.532.532 0 0 0 0 .75l6.583 6.593-6.583 6.606a.532.532 0 0 0 0 .75.527.527 0 0 0 .747 0L8.349 8.38a.519.519 0 0 0 .155-.375.54.54 0 0 0-.155-.375L1.406.662A.516.516 0 0 0 .659.65z"
fill="#50545B" fill-rule="evenodd"></path>
</svg>
</span>
</a>
</div>
</li>
</ul>
</div>
</li>
<li class="cat">
<button class="accordion ac2" data-parent="#myGroup"><a href="#y">sup drop 2</a></button>
<div class="panel">
<ul class="cat">
<li class="cat">
<div>
<a class="cat" href="#x">
<span class="_33353_3KdIO">
PC</span><span class="_3b1e8_3gyVo">
<svg height="16" viewBox="0 0 9 16" width="9"
xmlns="http://www.w3.org/2000/svg" class=""
name="chevron-next-outline">
<path
d="M.659.65a.532.532 0 0 0 0 .75l6.583 6.593-6.583 6.606a.532.532 0 0 0 0 .75.527.527 0 0 0 .747 0L8.349 8.38a.519.519 0 0 0 .155-.375.54.54 0 0 0-.155-.375L1.406.662A.516.516 0 0 0 .659.65z"
fill="#50545B" fill-rule="evenodd"></path>
</svg>
</span>
</a>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
请给我任何帮助,
还请注意,如某些示例所示,我尝试了data-parent="#accordion"
方法,但这没有用
这是一个工作的jsfiddle
https://jsfiddle.net/k7f38es6/
谢谢
答案 0 :(得分:1)
您可以执行以下操作。我还没有完全测试过,但这应该可以。 希望这会有所帮助。
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].addEventListener("click", function() {
resetAccordian(this);
var panel = this.nextElementSibling;
if (panel.style.display === "block") {
panel.style.display = "none";
this.classList.remove("active");
} else {
panel.style.display = "block";
}
});
}
resetAccordian = (originalTarget) => {
let accordians = Object.values(acc);
accordians.forEach( data => {
if (data != originalTarget) {
let target = data.nextElementSibling;
data.classList.remove("active");
if (target.style.display === "block") {
target.style.display = "none";
}
} else {
data.classList.add("active");
}
}, originalTarget);
}
答案 1 :(得分:1)
我已经修改了您原来的小提琴,以实现我认为您想要实现的目标。
这是代码的关键:
function openAccordionWindow(){
for( var closeIndex = 0; closeIndex < acc.length; closeIndex++){
acc[closeIndex].classList.remove('active');
var panel = acc[closeIndex].nextElementSibling;
console.log(panel);
panel.style.display = 'none';
}
this.classList.add("active");
var panel = this.nextElementSibling;
panel.style.display = 'block';
expandParents(panel);
}
function expandParents(target){
var parent = target.parentElement;
if(parent.id==='accordion'){
return;
}
if(parent.classList.contains('panel')){
parent.style.display = 'block';
}
parent.classList.add('tagged');
expandParents(parent);
}
通常是我的处理方式:
存在一些效率问题,但是它将实现您想要的目标,并且不会有太大的问题,因为您永远不会真正拥有那么多手风琴。
但是,有些注释使将来变得更容易。
编辑 -添加了额外的代码以扩展子菜单,并扩展了其子菜单的解释。