Bootstrap 4手风琴点击关闭时反复展开问题求助
Bootstrap 4手风琴面板关闭时
show类被移除后重新添加的问题 我使用Bootstrap 4官方文档中的手风琴示例代码时,遇到功能异常:尝试关闭已展开的面板时,面板元素的show类会被移除后立刻重新添加,导致面板无法正常关闭。我已经通过开发者工具的属性修改断点调试,但未能定位问题根源。
官方复制的代码片段
<div class="accordion" id="accordionExample"> <div class="card"> <div class="card-header" id="headingOne"> <h5 class="mb-0"> <button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne"> Collapsible Group Item #1 </button> </h5> </div> <div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordionExample"> <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> <div class="card"> <div class="card-header" id="headingTwo"> <h5 class="mb-0"> <button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"> Collapsible Group Item #2 </button> </h5> </div> <div id="collapseTwo" class="collapse" aria-labelledby="headingTwo" data-parent="#accordionExample"> <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> <div class="card"> <div class="card-header" id="headingThree"> <h5 class="mb-0"> <button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree"> Collapsible Group Item #3 </button> </h5> </div> <div id="collapseThree" class="collapse" aria-labelledby="headingThree" data-parent="#accordionExample"> <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> </div>
问题相关截图



排查建议
- 验证依赖加载顺序与版本:确保jQuery、Popper.js、Bootstrap JS按顺序加载,且版本符合Bootstrap 4.1的要求(jQuery ≥1.9.1,Popper.js ≥1.14),避免重复加载同一脚本。
- 排查自定义脚本干扰:临时移除页面中其他自定义JS代码,测试手风琴功能是否恢复,排查是否存在事件绑定或DOM操作冲突。
- 检查HTML结构正确性:确认
data-parent属性值与手风琴容器id完全匹配,面板id和按钮data-target一一对应,页面无重复id。 - 测试纯净环境:在仅加载Bootstrap官方CSS和JS的空白页面中运行这段代码,排查第三方库的样式/脚本冲突。
- 查看控制台错误:打开浏览器Console面板,检查是否存在JS报错(如未定义变量、DOM查找失败等),这类错误可能导致插件逻辑执行异常。
内容的提问来源于stack exchange,提问作者developerJ
相关产品推荐
相关产品推荐

