简体   繁体   English

使用Bootstrap3折叠div

[英]Collapse of div using Bootstrap3

I have four buttons in my page, and on the click of each one of them , it should reveal a div, and should close the open divs. 我的页面上有四个按钮,单击每个按钮,它应该显示一个div,并应该关闭打开的div。

I have got to open the corresponding div, but it does not close the ones which are open. 我必须打开相应的div,但它不会关闭打开的div。

Here is the code that I have done so far http://www.bootply.com/DWw9JbGFex 这是到目前为止我完成的代码http://www.bootply.com/DWw9JbGFex

Thanks for your help in advance. 感谢您的帮助。

Binding to the collapse event and hiding the rest of the collapsible elements using jQuery will do the trick. 绑定到崩溃事件并使用jQuery隐藏其余可折叠元素将达到目的。 See Boostrap javascript docs 参见Boostrap javascript文档

var $sharegroup = $('#sharegroup');
$sharegroup.on('show.bs.collapse','.collapse', function() {
    $sharegroup.find('.collapse.in').collapse('hide');
});

See the updated bootply 查看更新的启动
http://www.bootply.com/5oGRhtqdfw http://www.bootply.com/5oGRhtqdfw

Simple hide/show using jquery/bootstrap: 使用jquery / bootstrap进行简单的隐藏/显示:

HTML 的HTML

        <button type="button" class="btn btn-primary div-toggler" name="div1">Expose Div 1</button>
        <button type="button" class="btn btn-primary div-toggler" name="div2">Expose Div 2</button>
        <button type="button" class="btn btn-primary div-toggler" name="div3">Expose Div 3</button>
        <button type="button" class="btn btn-primary div-toggler" name="div4">Expose Div 4</button>

        <div id="div1" class="target-div" style="display: none;" >Div 1</div>
        <div id="div2" class="target-div" style="display: none;" >Div 2</div>
        <div id="div3" class="target-div" style="display: none;" >Div 3</div>
        <div id="div4" class="target-div" style="display: none;" >Div 4</div>

JavaScript 的JavaScript

$(document).ready(function () {
    $('.div-toggler').click(function (e) {
        var btnName = $(e.currentTarget).prop('name');
        $('.target-div').hide();
        $('#' + btnName).fadeIn(300);
    });
});

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

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