[英]Accordion - close one item when opening another one
我想在另一個項目打開時自動關閉一個項目,因此在任何時候都只有一個項目處於打開狀態。
HTML
<ons-list-item tappable class="accordion" onclick="fn.toggle(this)">List 1</ons-list-item>
<div class="panel">
<ons-list-item tappable>Item 1</ons-list-item>
<ons-list-item tappable>Item 2</ons-list-item>
</div>
JS
window.fn = {};
window.fn.toggle = function(el) {
el.classList.toggle("active");
el.nextElementSibling.classList.toggle("show");
}
這是答案:
window.fn = {};
window.fn.toggle = function(el) {
var elems = document.querySelectorAll(".accordion.active");
[].forEach.call(elems, function(els) {
if (el != els) {
els.classList.toggle("active");
els.nextElementSibling.classList.toggle("show");
}
});
el.classList.toggle("active");
el.nextElementSibling.classList.toggle("show");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.