简体   繁体   English

使用 javascript 展开整个手风琴

[英]Expand entire accordion using javascript

I am trying to incorporate a button that expands all of my accordion at once.我正在尝试合并一个按钮,可以一次展开我所有的手风琴。 I have tried the following but the button does not do anything.我尝试了以下操作,但该按钮不执行任何操作。 Any help would be appreciated as I cannot find the error.任何帮助将不胜感激,因为我找不到错误。

I am trying to use the following button:我正在尝试使用以下按钮:

<button id="toggleAccordions" class="btn btn-primary" type="button" data-toggle="collapse">Open / Close</button>

To expand all aspects of the following accordion:扩展以下手风琴的所有方面:

<div id="a1" role="tablist" class="accordion extend-right0">
    <div class="card">
        <div class="card-header" role="tab" id="a12010~2019heading">
            <h3 class="card-title">
                <a class="collapsed" data-toggle="collapse" href="#a12010~2019" aria-expanded="true" aria-controls="a12010~2019">
                    <div class="container"><span></span>2010~2019</div>
                </a>
            </h3>
        </div>
        <div id="a12010~2019" class="collapse show" role="tabpanel" aria-labelledby="a12010~2019heading" data-parent="#a1">
            <div class="card-body">
                <div class="container">
                    <h4>2010~2012</h4>
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th scope="col"></th>
                                <th scope="col">Position</th>
                                <th scope="col">Name</th>
                                <th scope="col">Programme</th>
                                <th scope="col">Graduation</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <th scope="row"></th>
                                <td>President</td>
                                <td>Name</td>
                                <td>Ph.D</td>
                                <td>20</td>
                            </tr>
                        </tbody>
                    </table>
                    <h4>2012~2014</h4>
                </div>
            </div>
        </div>
    </div>
    <div class="card">
        <div class="card-header" role="tab" id="a12000~2009heading">
            <h3 class="card-title">
                <a class="collapsed" data-toggle="collapse" href="#a12000~2009" aria-expanded="false" aria-controls="a12000~2009">
                    <div class="container"><span></span>2000~2009</div>
                </a>
            </h3>
        </div>
        <div id="a12000~2009" class="collapse" role="tabpanel" aria-labelledby="a12000~2009heading" data-parent="#a1">
            <div class="card-body">
                <div class="container">
                    <h4>2000~2001</h4>
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th scope="col"></th>
                                <th scope="col">Position</th>
                                <th scope="col">Name</th>
                                <th scope="col">Programme</th>
                                <th scope="col">Graduation</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <th scope="row"></th>
                                <td>President</td>
                                <td>name</td>
                                <td>Ph.D.</td>
                                <td>9 </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    <div class="card">
    </div>
</div>

Using the following js, but it does not expand:使用下面的js,但是没有展开:

$(function() {
$('#toggleAccordions').on('click', function(e) {

$('.accordion .collapse').toggleClass('show');
})
});

I see no issue with the code, I have prepared a small demo.我看代码没有问题,我准备了一个小演示。

Edit: The issue was with loading jquery after the js function being executed.编辑:问题在于在执行 js function 之后加载 jquery。 Making jquery load before the function execution worked.在 function 执行工作之前使 jquery 加载。

 $(function() { $('#toggleAccordions').on('click', function(e) { $('.accordion.collapse').toggleClass('show'); }) });
 .collapse { display: none; }.collapse.show { display: block; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="toggleAccordions" class="btn btn-primary" type="button" data-toggle="collapse">Open / Close</button> <div id="a1" role="tablist" class="accordion extend-right0"> <div class="card"> <div class="card-header" role="tab" id="a12010~2019heading"> <h3 class="card-title"> <a class="collapsed" data-toggle="collapse" href="#a12010~2019" aria-expanded="true" aria-controls="a12010~2019"> <div class="container"><span></span>2010~2019</div> </a> </h3> </div> <div id="a12010~2019" class="collapse" role="tabpanel" aria-labelledby="a12010~2019heading" data-parent="#a1"> <div class="card-body"> <div class="container"> <h4>2010~2012</h4> <table class="table table-hover"> <thead> <tr> <th scope="col"></th> <th scope="col">Position</th> <th scope="col">Name</th> <th scope="col">Programme</th> <th scope="col">Graduation</th> </tr> </thead> <tbody> <tr> <th scope="row"></th> <td>President</td> <td>Name</td> <td>Ph.D</td> <td>20</td> </tr> </tbody> </table> <h4>2012~2014</h4> </div> </div> </div> </div> <div class="card"> <div class="card-header" role="tab" id="a12000~2009heading"> <h3 class="card-title"> <a class="collapse" data-toggle="collapse" href="#a12000~2009" aria-expanded="false" aria-controls="a12000~2009"> <div class="container"><span></span>2000~2009</div> </a> </h3> </div> <div id="a12000~2009" class="collapse" role="tabpanel" aria-labelledby="a12000~2009heading" data-parent="#a1"> <div class="card-body"> <div class="container"> <h4>2000~2001</h4> <table class="table table-hover"> <thead> <tr> <th scope="col"></th> <th scope="col">Position</th> <th scope="col">Name</th> <th scope="col">Programme</th> <th scope="col">Graduation</th> </tr> </thead> <tbody> <tr> <th scope="row"></th> <td>President</td> <td>name</td> <td>Ph.D.</td> <td>9 </td> </tr> </tbody> </table> </div> </div> </div> </div> <div class="card"> </div> </div>

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

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