Bootstrap 手风琴 aria-expanded 不起作用

Posted

技术标签:

【中文标题】Bootstrap 手风琴 aria-expanded 不起作用【英文标题】:Bootstrap accordion aria-expanded doesnt work 【发布时间】:2019-08-09 21:08:39 【问题描述】:

我正在使用 bootstrap 4 手风琴。我希望作为初始状态让所有卡片都折叠起来。我将 aria-expanded 设置为 false,但所有卡片都显示为已展开。

<div id="accordion">
            <!-- ko foreach: $root.Addons() -->
            <div class="card">
                <div class="card-header" id="headingOne">
                    <h5 class="mb-0">
                        <button class="btn btn-link" data-toggle="collapse" aria-expanded="false" data-bind="text:name, attr:  'data-target': '#collapsible' +  $index(),'aria-controls': 'collapsible' +  $index() "></button>
                    </h5>
                </div>
                <div data-bind="attr:  id: 'collapsible' + $index() " class="collapse show" aria-labelledby="headingOne" data-parent="#accordion">
                    <div class="card-body">
                        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
                    </div>
                </div>
            </div>
            <!-- /ko -->
        </div>

我做错了什么?

【问题讨论】:

【参考方案1】:

您需要更新处于折叠状态的类。

1) 在按钮元素中添加类collapsedbtn btn-link

2) 从 div 元素中的 collapse show 中删除类 show

我已使用上述更改更新了代码。它最初应该使手风琴处于折叠状态。

<div id="accordion">
            <!-- ko foreach: $root.Addons() -->
            <div class="card">
                <div class="card-header" id="headingOne">
                    <h5 class="mb-0">
                        <button class="btn btn-link collapsed" data-toggle="collapse" aria-expanded="false" data-bind="text:name, attr:  'data-target': '#collapsible' +  $index(),'aria-controls': 'collapsible' +  $index() "></button>
                    </h5>
                </div>
                <div data-bind="attr:  id: 'collapsible' + $index() " class="collapse" aria-labelledby="headingOne" data-parent="#accordion">
                    <div class="card-body">
                        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
                    </div>
                </div>
            </div>
            <!-- /ko -->
        </div>

【讨论】:

以上是关于Bootstrap 手风琴 aria-expanded 不起作用的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Jquery 如何更改 dom 元素(Bootstrap)的 aria-expanded="false" 部分?

bootstrap实现手风琴功能(树形列表)

Bootstrap 4:如何创建一个带有手风琴的下拉菜单?

Bootstrap手风琴,点击时滚动到活动(打开)手风琴的顶部?

Bootstrap 4:手风琴不会折叠卡片

ui.bootstrap 手风琴容器宽度问题