简体   繁体   English

面板中的Bootstrap面板:单击内部面板时,两个字形都切换

[英]Bootstrap panel in a panel: Both glyphicons toggle when inner panel is clicked

I have a bootstrap panel with another panel in it. 我有一个引导面板,其中有另一个面板。 Both have in the panel header a glyphicon. 两者在面板标题中都有一个glyphicon。 The panels are collapsed by default. 面板默认为折叠状态。 When the panels are collapsed, the glyphicon-unchecked is set. 当面板折叠时,设置了未经选中的glyphicon。 When I click on this glyphicon, the panel uncollapses. 当我单击该glyphicon时,面板会崩溃。 I wrote following JavaScript function for this: 我为此编写了以下JavaScript函数:

$('#outerPanelGlyph').click(function () {
    $('#outerPanelBody').collapse('toggle');
});

This works properly. 这可以正常工作。 When I click this, the outer panel gets uncollapsed but the inner is still collapsed. 当我单击此按钮时,外部面板不会折叠,但内部面板仍然折叠。 I wrote methods, so the glyphicon gets exchanged with "glyphicon-check" on uncollapse and vice versa: 我编写了方法,因此在未折叠时将glyphicon与“ glyphicon-check”交换,反之亦然:

$('#outerPanelBody').on('show.bs.collapse', function () {
    $('#outerPanelGlyph').removeClass("glyphicon-unchecked").addClass("glyphicon-check");
});

$('#outerPanelBody').on('hide.bs.collapse', function () {
    $('#outerPanelGlyph').removeClass("glyphicon-check").addClass("glyphicon-unchecked");
 });

I wrote exactly the same code as above for the inner panel. 我为内部面板编写了与上面完全相同的代码。 When I click on the glyph of the inner panel, the inner panel gets uncollapsed and the glyph exchanged. 当我单击内部面板的字形时,内部面板不会折叠,并且交换了该字形。 But, when I click on the glyph of the inner panel again, the inner panel collapses and both the glyph of the inner panel and of the outer panel get exchanged. 但是,当我再次单击内部面板的字形时,内部面板会折叠,并且内部面板和外部面板的字形都会交换。 I just want the inner panel glyph exchanged when I click on the glyphicon of the inner panel. 我只希望在单击内部面板的字形图标时交换内部面板的字形。

Here is the HTML-Code: 这是HTML代码:

<div class="panel panel-dark" id="outerPanel">
    <div class="panel-heading">
        <span class="pull-right"><i class="glyphicon glyphicon-unchecked" id="outerPanelGlyph"></i></span>
    </div>
        ....some code....
    <div class="collapse panel-body" id="outerPanelBody">
        <div class="panel panel-default" id="innerPanel">
            <div class="panel-heading panel-dark">
                <span class="pull-right"><i class="glyphicon glyphicon-unchecked" id="innerPanelGlyph"></i></span>
            </div>
            <div class="collapse panel-body" id="innerPanelBody">
                ....some code....
            </div>
        </div>
    </div>
</div>

When you click the inner panel, you also click the outer panel so you need to stop propagation in the inner panel click event otherwise both events will be fired. 当您单击内部面板时,也会单击外部面板,因此您需要在内部面板click事件中停止传播,否则将触发两个事件。

$('#outerPanelBody').on('show.bs.collapse', function (e) {
    e.stopPropagation();
    $('#outerPanelGlyph').removeClass("glyphicon-unchecked").addClass("glyphicon-check");
});

$('#outerPanelBody').on('hide.bs.collapse', function (e) {
    e.stopPropagation();
    $('#outerPanelGlyph').removeClass("glyphicon-check").addClass("glyphicon-unchecked");
});

The answer from Steve Harris does work, but he wrote it in the wrong method. 史蒂夫·哈里斯(Steve Harris)的答案确实有用,但是他用错误的方法写了出来。 Here are my new methods which toggle the glyph: 这是我切换字形的新方法:

$('#outerPanelBody').on('show.bs.collapse', function (e) {
    e.stopPropagation();
    $('#outerPanelGlyph').removeClass("glyphicon-unchecked").addClass("glyphicon-check");
});

$('#outerPanelBody').on('hide.bs.collapse', function (e) {
    e.stopPropagation();
    $('#outerPanelGlyph').removeClass("glyphicon-check").addClass("glyphicon-unchecked");
});

Thank you very much for the fast help! 非常感谢您的快速帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM