我有一个可以伸缩的手风琴。该手风琴还具有用于表示手风琴何时使用 +/- 展开或折叠的图标。问题是,假设我展开第 1 项,然后展开第 2 项。第 1 项折叠得很好,但图标仍然是减号,但它应该是加号,因为它已折叠。所以问题是,它没有正确切换。这是代码:https : //jsfiddle.net/y35rbzsd/
var coll = document.getElementsByClassName("accordion-toggle");
for (var i = 0; i < coll.length; i++) {
coll[i].addEventListener("click", function(evnt) {
const currClassList = evnt.target.classList;
if (currClassList.contains('collapsed')) {
evnt.target.classList.remove("collapsed");
evnt.target.querySelector('.fa').classList.remove("fa-minus");
evnt.target.querySelector('.fa').classList.add("fa-plus");
var content = evnt.target.nextElementSibling;
content.style.maxHeight = null;
} else {
for (var j = 0; j < coll.length; j++) {
coll[j].classList.remove("collapsed")
coll[j].nextElementSibling.style.maxHeight = null;
}
this.classList.toggle("collapsed");
evnt.target.querySelector('.fa').classList.remove("fa-plus");
evnt.target.querySelector('.fa').classList.add("fa-minus");
var content = this.nextElementSibling;
if (content.style.maxHeight){
content.style.maxHeight = null;
} else {
content.style.maxHeight = content.scrollHeight + "px";
}
}
});
}
相关分类