几个关键的属性
.collapse 用法class="collapse" 该元素被折叠
.collapse.in 用法class="collapse in" 展开该元素
data-parent="#selector" 保证parent下只有1个元素处于展开状态
这个属性的作用:展开当前元素时,折叠其它暂开的元素,实现parent下只有1个元素处于展开状态
如果不加该属性,则所有可折叠元素的折叠状态互不影响
设置id属性,然后用button或anchor标签,使用id选择器控制目标
通过按钮控制折叠
在按钮上设置data属性,如
data-toggle="collapse" data-target="#collapseFirst" data-parent="#accordion"
使用javascript进行操作,可以指定toggle和parent属性
$(".collapse").collapse()
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Default buttons from Bootstrap 3</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="../../bootstrap-3.0.0/dist/css/bootstrap.min.css"rel="stylesheet" media="screen">
<style>
body {padding: 50px; /*边距*/
}
</style>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]><script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script><script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script><![endif]-->
</head>
<body><!-- 通过按钮的data-target属性控制目标是否折叠(控制折叠方式一) --><button type="button" class="btn btn-danger" data-toggle="collapse" data-target="#collapseFirst" data-parent="#accordion">使用按钮折叠A</button><div class="panel-group" id="accordion"><div class="panel panel-default"><div class="panel-heading"><h4 class="panel-title"><!-- 超链接控制折叠 --><a data-toggle="collapse" href="#collapseFirst" data-parent="#accordion">使用超链接折叠A</a></h4></div><!-- 初始处于展开状态--><div id="collapseFirst" class="panel-collapse collapse in"><div class="panel-body">AAAAAAAAAAAAAAAAAAAAAAAA</div></div><div class="panel-heading"><h4 class="panel-title"><!-- 超链接控制折叠 --><a data-toggle="collapse" href="#collapseSecond" data-parent="#accordion">使用超链接折叠B</a></h4></div><!-- 初始处于被折叠状态--><div id="collapseSecond" class="panel-collapse collapse"><div class="panel-body">BBBBBBBBBBBBBBBBBBBBBBBBB</div></div><div class="panel-heading"><h4 class="panel-title"><!-- 超链接控制折叠 --><a data-toggle="collapse" href="#collapseThird" data-parent="#accordion">使用超链接折叠C</a></h4></div><!-- 初始处于被折叠状态--><div id="collapseThird" class="panel-collapse collapse"><div class="panel-body">CCCCCCCCCCCCCCCCCCCCCCCCCC</div></div></div></div><!-- jQuery (necessary for Bootstrap's JavaScript plugins) --><script src="../../jquery/jquery-10.0.2.js"></script><!-- Include all compiled plugins (below), or include individual files as needed --><script src="../../bootstrap-3.0.0/dist/js/bootstrap.min.js"></script>
</body>
</html>