Bootstrap 3 折叠手风琴:折叠所有作品,但无法在保持数据父级的同时展开所有作品 [英] Bootstrap 3 collapse accordion: collapse all works but then cannot expand all while maintaining data-parent

查看:34
本文介绍了Bootstrap 3 折叠手风琴:折叠所有作品,但无法在保持数据父级的同时展开所有作品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Bootstrap 3 并尝试设置以下手风琴/折叠结构:

  1. Onload:组中的每个手风琴面板都完全折叠并按文档/预期运行.

  2. 按钮点击:每个手风琴面板都会展开,点击开关没有效果(包括 URL 锚点效果).

  3. 再次点击按钮:所有面板返回加载状态;全部折叠并可以正常点击.

我已经完成了第 2 步,但是当我在第 3 步再次单击该按钮时,它没有任何效果.我也没有看到 Chrome 开发工具中报告的控制台错误或通过我的本地 JSHint 运行代码.

我希望每次点击按钮时都可以重复此循环.

我在这里http://bootply.com/98140和这里http://jsfiddle.net/A9vCx/

我很想知道我做错了什么,我很感激建议.谢谢!

我的 HTML:

<div id="collapseOne" class="panel-collapse collapse"><div class="panel-body">Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid.

<div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">可折叠组项目 #2</a>

<div id="collapseTwo" class="panel-collapse collapse"><div class="panel-body">Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid.

<div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title"><a data-toggle="collapse" data-parent="#accordion" href="#collapseThree">可折叠组项目 #3</a>

<div id="collapseThree" class="panel-collapse collapse"><div class="panel-body">Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid.

我的JS:

$(function() {var $active = true;$('.panel-title > a').click(function(e) {e.preventDefault();});$('.collapse-init').on('click', function() {如果(!$活动){$active = 真;$('.panel-title > a').attr('data-toggle', 'collapse');$('.panel-collapse').collapse({'toggle': true, 'parent': '#accordion'});$(this).html('点击禁用手风琴行为');} 别的 {$active = 假;$('.panel-collapse').collapse({'toggle': true, 'parent': '#accordion'});$('.panel-title > a').removeAttr('data-toggle');$(this).html('点击以启用手风琴行为');}});});

解决方案

更新答案

尝试打开设置为手风琴的折叠控件的多个面板,即使用 data-parent 属性集,可能会出现很多问题和错误(请参阅 以编程方式打开面板后打开多个面板)

相反,最好的方法是:

  1. 允许每个面板单独切换
  2. 然后,在适当的情况下手动强制执行手风琴行为.


要允许每个面板单独切换,在 data-toggle="collapse" 元素上,设置 data-target 属性到 .collapse 面板 ID 选择器(而不是将 data-parent 属性设置为父控件.您可以在问题 修改 Twitter Bootstrap 折叠插件以保持手风琴打开.

粗略地说,每个面板应如下所示:

<div class="panel-heading"><h4 class="panel-title";数据切换=折叠"数据目标=#collapseOne">可折叠组项目 #1

<div id="collapseOne";class=面板折叠折叠"><div class="panel-body"></div>


要手动强制执行手风琴行为,您可以为在任何面板显示之前发生的折叠显示事件创建处理程序.使用它来确保在显示所选面板之前关闭任何其他打开的面板(请参阅此打开多个面板的答案).您还将只希望在面板处于活动状态时执行代码.为此,请添加以下代码:

$('#accordion').on('show.bs.collapse', function () {if (active) $('#accordion .in').collapse('hide');});


然后使用 showhide 来切换每个面板的可见性,并使用 data-toggle 来启用和禁用控件.

$('#collapse-init').click(function () {如果(活动){活动 = 假;$('.panel-collapse').collapse('show');$('.panel-title').attr('data-toggle', '');$(this).text('启用手风琴行为');} 别的 {活动 = 真;$('.panel-collapse').collapse('隐藏');$('.panel-title').attr('data-toggle', 'collapse');$(this).text('禁用手风琴行为');}});

jsFiddle 中的工作演示

I'm using Bootstrap 3 and trying to setup the following accordion/collapse structure:

  1. Onload: Each accordion panel in a group is fully collapsed and functions as documented/expected.

  2. Button click: Each accordion panel expands and clicking the toggles has no effect (including URL anchor effects).

  3. Another button click: All panels return to onload state; all collapsed and clickable as normal.

I've made it to step 2, but when I click the button again at step 3 it has no effect. I also see no console errors reported in Chrome Dev Tools or by running the code through my local JSHint.

I'd like this cycle to be repeatable each time the button is clicked.

I've setup my code here http://bootply.com/98140 and here http://jsfiddle.net/A9vCx/

I'd love to know what I'm doing wrong and I appreciate suggestions. Thank you!

My HTML:

<button class="collapse-init">Click to disable accordion behavior</button>
<br><br>
<div class="panel-group" id="accordion">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
          Collapsible Group Item #1
        </a>
      </h4>
    </div>
    <div id="collapseOne" class="panel-collapse collapse">
      <div class="panel-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid.
      </div>
    </div>
  </div>
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
          Collapsible Group Item #2
        </a>
      </h4>
    </div>
    <div id="collapseTwo" class="panel-collapse collapse">
      <div class="panel-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid.
      </div>
    </div>
  </div>
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree">
          Collapsible Group Item #3
        </a>
      </h4>
    </div>
    <div id="collapseThree" class="panel-collapse collapse">
      <div class="panel-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid.
      </div>
    </div>
  </div>
</div>

My JS:

$(function() {

  var $active = true;

  $('.panel-title > a').click(function(e) {
    e.preventDefault();
  });

  $('.collapse-init').on('click', function() {
    if(!$active) {
      $active = true;
      $('.panel-title > a').attr('data-toggle', 'collapse');
      $('.panel-collapse').collapse({'toggle': true, 'parent': '#accordion'});
      $(this).html('Click to disable accordion behavior');
    } else {
      $active = false;
      $('.panel-collapse').collapse({'toggle': true, 'parent': '#accordion'});
      $('.panel-title > a').removeAttr('data-toggle');
      $(this).html('Click to enable accordion behavior');
    }
  });

});

解决方案

Updated Answer

Trying to open multiple panels of a collapse control that is setup as an accordion i.e. with the data-parent attribute set, can prove quite problematic and buggy (see this question on multiple panels open after programmatically opening a panel)

Instead, the best approach would be to:

  1. Allow each panel to toggle individually
  2. Then, enforce the accordion behavior manually where appropriate.


To allow each panel to toggle individually, on the data-toggle="collapse" element, set the data-target attribute to the .collapse panel ID selector (instead of setting the data-parent attribute to the parent control. You can read more about this in the question Modify Twitter Bootstrap collapse plugin to keep accordions open.

Roughly, each panel should look like this:

<div class="panel panel-default">
   <div class="panel-heading">
         <h4 class="panel-title"
             data-toggle="collapse" 
             data-target="#collapseOne">
             Collapsible Group Item #1
         </h4>
    </div>
    <div id="collapseOne" 
         class="panel-collapse collapse">
        <div class="panel-body"></div>
    </div>
</div>


To manually enforce the accordion behavior, you can create a handler for the collapse show event which occurs just before any panels are displayed. Use this to ensure any other open panels are closed before the selected one is shown (see this answer to multiple panels open). You'll also only want the code to execute when the panels are active. To do all that, add the following code:

$('#accordion').on('show.bs.collapse', function () {
    if (active) $('#accordion .in').collapse('hide');
});


Then use show and hide to toggle the visibility of each of the panels and data-toggle to enable and disable the controls.

$('#collapse-init').click(function () {
    if (active) {
        active = false;
        $('.panel-collapse').collapse('show');
        $('.panel-title').attr('data-toggle', '');
        $(this).text('Enable accordion behavior');
    } else {
        active = true;
        $('.panel-collapse').collapse('hide');
        $('.panel-title').attr('data-toggle', 'collapse');
        $(this).text('Disable accordion behavior');
    }
});

Working demo in jsFiddle

这篇关于Bootstrap 3 折叠手风琴:折叠所有作品,但无法在保持数据父级的同时展开所有作品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆