如何在没有数据标记的情况下为手风琴使用bootstrap 3折叠?

时间:2013-11-21 13:13:31

标签: javascript jquery twitter-bootstrap twitter-bootstrap-3

我正在尝试在没有所有data-属性标记的情况下为手风琴使用bootstrap折叠。我看到它在创建时切换,但我希望panel-title中的两个锚点控制崩溃。我知道如何使用jQuery手动执行此操作,但我认为我应该能够通过一次调用.collapse()来完成此操作。

JS

$('.collapse').collapse();
//$('.t-title,.t-toggle').collapse(); // also tried this with no luck

HTML

<!-- notices start -->
<div class="row spaced" id="notice">
  <div class="col">
    <div class="panel-group" id="accordion">
      <!-- start update -->
      <div class="panel panel-warning">
        <div class="panel-heading">
          <h4 class="panel-title">
            <a class="t-title" href="#notice-1">title</a>
            <a class="t-toggle" href="#notice-1">toggle</a>
          </h4>
        </div>
        <div id="notice-1" class="panel-collapse collapse in">
          <div class="panel-body"><p>content</p><p>content</p><p>content</p></div>
        </div>
      </div>
      <!-- / end update -->
      <!-- start update -->
      <div class="panel panel-success">
        <div class="panel-heading">
          <h4 class="panel-title">
            <a class="t-title" href="#notice-2">title</a>
            <a class="t-toggle" href="#notice-2">toggle</a>
          </h4>
        </div>
        <div id="notice-2" class="panel-collapse collapse">
          <div class="panel-body"><p>content</p><p>content</p><p>content</p></div>
        </div>
      </div>
      <!-- / end update -->
      <!-- start update -->
      <div class="panel panel-danger">
        <div class="panel-heading">
          <h4 class="panel-title">
            <a class="t-title" href="#notice-3">title</a>
            <a class="t-toggle" href="#notice-3">toggle</a>
          </h4>
        </div>
        <div id="notice-3" class="panel-collapse collapse">
          <div class="panel-body"><p>content</p><p>content</p><p>content</p></div>
        </div>
      </div>
      <!-- / end update -->
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:2)

您不能将data-标记放在一边,因为collapse()函数用于初始化折叠组件,它不是事件触发器。

如果您不想手动设置每个data-toggle属性,则至少可以使用此行:

$('#accordion').find('.t-title, .t-toggle').attr('data-toggle', 'collapse');

<强> Bootply