简体   繁体   English

当另一个手风琴打开时,如何让手风琴折叠?

[英]How do I make my accordion collapse when another is open?

I want other accordions to close when one is open. 我希望其他手风琴在打开时关闭。 I tried using data-toggles with bootstrap but it didnt help. 我尝试使用带有引导程序的数据切换,但它没有帮助。 Here is my complete source code on Github . 这是我在Github上的完整源代码。

<button class="accordion">Bedroom & Living Room</button>
                <div class="panel">
                    <p>Wipe down tables and chairs.<br>
                    Removing dirt from carpets using high-powered vacuums.<br>
                    Removing garbage and debris.</p>
                </div> <br>
                <button class="accordion">Bathroom & Kitchen</button>
                <div class="panel">
                    <p>Wash Dishes<br>
                    Clean counters<br>
                    Stove</p>
                </div> <br>
                <button class="accordion">Extra Services</button>
                <div class="panel">
                    <p>Polished furniture.<br>
                    Dusting window sills and desk.<br>
                    Proper removal of dust and careful treatment to blinds and mini blinds.</p>
                </div> 



<script>
var acc = document.getElementsByClassName("accordion");
var i;

for (i = 0; i < acc.length; i++) {
    acc[i].onclick = function(){
        this.classList.toggle("active");
        var panel = this.nextElementSibling;
        if (panel.style.display === "block") {
            panel.style.display = "none";
        } else {
            panel.style.display = "block";
        }
    }
}
</script>

As i said Data-toggles didnt work. 正如我所说,数据切换不起作用。 Please tell me if I am doing anthing wrong. 如果我做错了,请告诉我。 You can try to edit it in my git hub and tell me if it works. 您可以尝试在我的git hub中编辑它,并告诉我它是否有效。

<div id="accordion">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapse1"><button class="accordion">Bedroom & Living Room</button></a>
            <div id="collapse1" class="panel-collapse collapse in">
            <div class="panel">
                <p>Wipe down tables and chairs.<br>
                Removing dirt from carpets using high-powered vacuums.<br>
                Removing garbage and debris.</p>
            </div>
            </div>
            <a data-toggle="collapse" data-parent="#accordion" href="#collapse2"><button class="accordion">Bathroom & Kitchen</button></a>
            <div id="collapse2" class="panel-collapse collapse in">
            <div class="panel">
                <p>Wash Dishes<br>
                Clean counters<br>
                Stove</p>
            </div>
            </div>
            <a data-toggle="collapse" data-parent="#accordion" href="#collapse3"><button class="accordion">Extra Services</button></a>
            <div id="collapse3" class="panel-collapse collapse in">
            <div class="panel">
                <p>Polished furniture.<br>
                Dusting window sills and desk.<br>
                Proper removal of dust and careful treatment to blinds and mini blinds.</p>
            </div>
            </div>  
            </div>

Since you're using bootstrap you may want to look at using their accordion: 既然你正在使用bootstrap,你可能想看看他们使用他们的手风琴:

https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_collapsible_accordion&stacked=h https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_collapsible_accordion&stacked=h

It does exactly what you want to do. 它完全符合您的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM