【问题标题】:How to prevent accordion from toggling when button in header is clicked?单击标题中的按钮时如何防止手风琴切换?
【发布时间】:2014-08-19 14:42:58
【问题描述】:

我正在使用 bootstrap 的手风琴类,并希望在标题中包含可以在不切换手风琴的情况下单击的按钮。这是示例 HTML:

<div class="body">
    <div class="panel panel-default" style="width: 80%">
        <div class="panel-heading" data-toggle="collapse" data-target="#container1" style="cursor: pointer;">
            <h4 class="panel-title" style="display: inline-block;">
               title
            </h4>
        <div style="float: right; width: 130px; text-align: right;"><span style="color: red;">test</span></div>
            <button type="button" class="btn btn-xs btn-primary" style="float: right;" onclick="foo(event);">Button</button>
    </div>
    <div id="container1" class="panel-collapse collapse">
        <div class="panel-body">
            panel content
        </div>
    </div>
    </div>
</div>

我尝试使用带有e.preventDefault();onclick 处理程序来阻止手风琴触发,但没有奏效:

window.foo = function(e) {
    console.log("foo");
    $(".body").append("<br>foo");
    e.preventDefault();
}

JSFIDDLE

如何防止点击按钮时触发手风琴?

【问题讨论】:

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


【解决方案1】:

你想停止事件冒泡,所以你使用stopPropagation

window.foo = function(e) {
    e.stopPropagation();
}

演示:http://jsfiddle.net/Bye8K/3/

【讨论】:

  • 提醒人们使用按钮(至少在 React 中)激活引导模式时,您必须在单击时停止模式中的传播,否则单击模式也会展开/折叠手风琴。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多