我正在研究嵌套的accordion.i希望当我关闭我的父母手风琴时,打开儿童手风琴会自动关闭。 这是我的javaScript函数:
var accc = document.getElementsByClassName("accordion");
var panell = document.getElementsByClassName('panel1');
var acc = document.getElementsByClassName("accordion_inner");
var panel = document.getElementsByClassName('panel2');
for (var i = 0; i < accc.length; i++) {
accc[i].onclick = function () {
var setClasses = !this.classList.contains('active');
setClass(accc, 'active', 'remove');
setClass(panell, 'show', 'remove');
if (setClasses) {
this.classList.toggle("active");
this.nextElementSibling.classList.toggle("show");
}
}
}
for (var i = 0; i < acc.length; i++) {
acc[i].onclick = function () {
var setClasses = !this.classList.contains('active');
setClass(acc, 'active', 'remove');
setClass(panel, 'show', 'remove');
if (setClasses) {
this.classList.toggle("active");
this.nextElementSibling.classList.toggle("show");
}
}
}function setClass(els, className, fnName) {
for (var i = 0; i < els.length; i++) {
els[i].classList[fnName](className);
}
}function setClass(els, className, fnName) {
for (var i = 0; i < els.length; i++) {
els[i].classList[fnName](className);
}
}