twitter-bootstrap Bootstrap 折叠面板默认关闭
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31608304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bootstrap collapse panel closed by default
提问by Dev java
I use a bootstrap panel and i added a span clickable icon : plus when panel body is open and minus when panel body is close, my problem now is how can i do to set it close by default(plus icon showed by default and when i click on it panel body will be opening)
我使用引导面板,并添加了一个跨度可点击图标:加上面板主体打开时和面板主体关闭时减去,我现在的问题是如何将其设置为默认关闭(加号图标默认显示,当我单击它面板主体将打开)
above my code to do this, thanks in advance for your help.
在我的代码上方执行此操作,在此先感谢您的帮助。
<html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script>
$(document).on('click', '.panel-heading span.clickable', function (e) {
var $this = $(this);
if (!$this.hasClass('panel-collapsed')) {
$this.parents('.panel').find('.panel-body').slideUp();
$this.addClass('panel-collapsed');
$this.find('i').removeClass('glyphicon-minus').addClass('glyphicon-plus');
} else {
console.log($this);
$this.parents('.panel').find('.panel-body').slideDown();
$this.removeClass('panel-collapsed');
$this.find('i').removeClass('glyphicon-plus').addClass('glyphicon-minus');
}
});
</script>
<style>
.clickable
{
cursor: pointer;
}
.clickable .glyphicon
{
background: rgba(0, 0, 0, 0.15);
display: inline-block;
padding: 6px 12px;
border-radius: 4px
}
.panel-heading span
{
margin-top: -23px;
font-size: 15px;
margin-right: -9px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">
Panel 1</h3>
<span class="pull-right clickable panel-collapsed in"><i class="glyphicon glyphicon-plus"></i></span>
</div>
<div class="panel-body">
Panel content</div>
</div>
</div>
</div>
</div>
</body>
</html>
采纳答案by ayadi baha
You just need to change this:
你只需要改变这个:
<div class="panel-body">
To this :
对此:
<div class="panel-body" style="display:none;">
And change the slideUp() by slideDown(), and the same for slideDown() by slideUp().
并通过slideDown()更改slideUp(),通过slideUp()更改slideDown()。
Also change this:
也改变这个:
<span class="pull-right clickable collapse in"><i class="glyphicon glyphicon-plus"></i></span>
To this:
对此:
<span class="pull-right clickable collapse in"><i class="glyphicon glyphicon-minus"></i></span>
Everything should work as you mentioned in your question now.
现在一切都应该像您在问题中提到的那样工作。
Here's a JSFiddle link : Try It
这是一个 JSFiddle 链接:试试看
回答by Denis Thomas
You could use the Bootstrap collapsecomponent which already has most of the logic you need:
您可以使用 Bootstrap折叠组件,它已经拥有您需要的大部分逻辑:
<div class="panel-heading">
<h3 class="panel-title">Panel 1</h3>
<button class="btn btn-primary pull-right" type="button"
data-toggle="collapse" data-target="#collapseExample">
<i class="glyphicon glyphicon-plus"></i>
</button>
</div>
<div class="collapse" id="collapseExample">
<div class="panel-body">Panel content</div>
</div>
In addition you only need a small JavaScript to change the button icon:
此外,您只需要一个小的 JavaScript 来更改按钮图标:
$(document).on('click', '.panel-heading button', function(e) {
var $this = $(this);
var icon = $this.find('i');
if (icon.hasClass('glyphicon-plus')) {
$this.find('i').removeClass('glyphicon-plus').addClass('glyphicon-minus');
} else {
$this.find('i').removeClass('glyphicon-minus').addClass('glyphicon-plus');
}
});
Working example:
工作示例:
<html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script>
$(document).on('click', '.panel-heading button', function(e) {
var $this = $(this);
var icon = $this.find('i');
if (icon.hasClass('glyphicon-plus')) {
$this.find('i').removeClass('glyphicon-plus').addClass('glyphicon-minus');
} else {
$this.find('i').removeClass('glyphicon-minus').addClass('glyphicon-plus');
}
});
</script>
<style>
.clickable {
cursor: pointer;
}
.clickable .glyphicon {
background: rgba(0, 0, 0, 0.15);
display: inline-block;
padding: 6px 12px;
border-radius: 4px
}
.panel-heading span {
margin-top: -23px;
font-size: 15px;
margin-right: -9px;
}
.panel-heading button {
margin-top: -25px;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">
Panel 1</h3>
<button class="btn btn-primary pull-right" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
<i class="glyphicon glyphicon-plus"></i>
</button>
</div>
<div class="collapse" id="collapseExample">
<div class="panel-body">
Panel content</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
回答by Ankur Mahajan
I am sharing a reference code for you, just follow the code, there is no need of any click function, bootstrap 'data-toggle="collapse"' handles it automatically.
给大家分享一个参考代码,跟着代码走,不需要任何点击功能,bootstrap 'data-toggle="collapse"' 自动处理。
CODE:
代码:
<div class="panel-group" id="steps">
<!-- Step 1 -->
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#steps" href="#stepOne"><SPAN TAG></a>
</h4>
</div>
<div id="stepOne" class="panel-collapse collapse">
<div class="panel-body">
<div class="form-group">
<label class="col-lg-2 control-label">Sub-Category Name</label>
</div>
</div>
</div>
</div>
</div>
Just put the span tag in place of and use class pull-right, the bootstrap icon which one you need http://getbootstrap.com/components/
只需将 span 标签代替并使用类 pull-right,即您需要的引导程序图标http://getbootstrap.com/components/
I hope this will work.
我希望这会奏效。

