2024-06-01 使用 js forEach 遍歷所有所屬的元素 發表於 2024-07-14 更新於 2024-07-23 分類於 OneDayOneNote 閱讀次數: 2024-06-01 使用 js forEach 遍歷所有所屬的元素 2024-06-01 使用 js forEach 遍歷所有所屬的元素在 click 事件處理器中遍歷所有 .collapsible 元素,將它們的內容全部閉合,然後再處理當前點擊的內容。 123456789// 先將所有的內容閉合 coll.forEach(function(item) { item.classList.remove("active"); var content = item.nextElementSibling; content.style.maxHeight = null; }); // 再處理當前點擊的內容 this.classList.toggle("active");