2024-06-01 使用 js forEach 遍歷所有所屬的元素

2024-06-01 使用 js forEach 遍歷所有所屬的元素

在 click 事件處理器中遍歷所有 .collapsible 元素,將它們的內容全部閉合,然後再處理當前點擊的內容。

1
2
3
4
5
6
7
8
9
// 先將所有的內容閉合
coll.forEach(function(item) {
item.classList.remove("active");
var content = item.nextElementSibling;
content.style.maxHeight = null;
});

// 再處理當前點擊的內容
this.classList.toggle("active");