我正在使用Bootstrap 3,我想创建一个单选按钮组,根据所选的单选按钮显示/隐藏内容。我希望尽可能使用所有HTML和数据属性。
以下是我的代码:
<div id="parent">
<input type="radio" name="group1" value="1" data-parent="#parent" data-toggle="collapse" data-target="#div1" />
<input type="radio" name="group1" value="2" data-parent="#parent" data-toggle="collapse" data-target="#div2" />
<div class="panel-collapse collapse in" id="div1">Content</div>
<div class="panel-collapse collapse in" id="div2">Content</div>
</div>
这将显示/ hode目标DIV就好了,但我需要它来关闭#Parent DIV内的所有其他DIV。
我知道你可以用面板做到这一点,但我从来没有看到它用单选按钮完成,而且我非常接近。
有什么想法吗?
答案 0 :(得分:7)
您可以组合面板和单选按钮。 只需使用单选按钮作为切换按钮,然后使用它来控制面板。
演示 http://www.bootply.com/Fdjc8kQiER#
源代码
<input type="radio" name="group1" value="1" data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
<input type="radio" name="group1" value="2" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
<input type="radio" name="group1" value="3" data-toggle="collapse" data-parent="#accordion" href="#collapseThree">
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
1. What is HTML?
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in">
<div class="panel-body">
<p>HTML stands for HyperText Markup Language. HTML is the main markup language for describing the structure of Web pages. <a href="http://www.tutorialrepublic.com/html-tutorial/" target="_blank">Learn more.</a></p>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
2. What is Bootstrap?
</h4>
</div>
<div id="collapseTwo" class="panel-collapse collapse">
<div class="panel-body">
<p>Bootstrap is a powerful front-end framework for faster and easier web development. It is a collection of CSS and HTML conventions. <a href="http://www.tutorialrepublic.com/twitter-bootstrap-tutorial/" target="_blank">Learn more.</a></p>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
3. What is CSS?
</h4>
</div>
<div id="collapseThree" class="panel-collapse collapse">
<div class="panel-body">
<p>CSS stands for Cascading Style Sheet. CSS allows you to specify various style properties for a given HTML element such as colors, backgrounds, fonts etc. <a href="http://www.tutorialrepublic.com/css-tutorial/" target="_blank">Learn more.</a></p>
</div>
</div>
</div>
</div>
唯一不完美的是,由于某种原因,单选按钮在您选择时不会更新。你再也看不到所选的了。 (不知道这是怎么发生的......带引导程序的东西?如果你知道如何解决,请发表评论,所以我可以编辑)
希望有所帮助
答案 1 :(得分:0)
实际上,您可以将jQuery的stopPropagation()与HTML代码一起使用。
<div class="bs-example">
<input class="radio" id="collapseOne" type="radio" name="group1" value="1" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" checked>
<input class="radio" id="collapseTwo" type="radio" name="group1" value="2" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
<input class="radio" id="collapseThree" type="radio" name="group1" value="3" data-toggle="collapse" data-parent="#accordion" href="#collapseThree">
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
1. What is HTML?
</h4>
</div>
<div id="collapseOnePanel" class="panel-collapse collapse in">
<div class="panel-body">
<p>HTML stands for HyperText Markup Language. HTML is the main markup language for describing the structure of Web pages. <a href="http://www.tutorialrepublic.com/html-tutorial/" target="_blank">Learn more.</a></p>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
2. What is Bootstrap?
</h4>
</div>
<div id="collapseTwoPanel" class="panel-collapse collapse">
<div class="panel-body">
<p>Bootstrap is a powerful front-end framework for faster and easier web development. It is a collection of CSS and HTML conventions. <a href="http://www.tutorialrepublic.com/twitter-bootstrap-tutorial/" target="_blank">Learn more.</a></p>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
3. What is CSS?
</h4>
</div>
<div id="collapseThreePanel" class="panel-collapse collapse">
<div class="panel-body">
<p>CSS stands for Cascading Style Sheet. CSS allows you to specify various style properties for a given HTML element such as colors, backgrounds, fonts etc. <a href="http://www.tutorialrepublic.com/css-tutorial/" target="_blank">Learn more.</a></p>
</div>
</div>
</div>
</div>
</div>
这是JS代码。
$('#collapseOne,#collapseTwo,#collapseThree').on('click', function (e) {
e.stopPropagation();
if(this.id == 'collapseOne'){
$('#collapseTwoPanel').collapse('hide');
$('#collapseThreePanel').collapse('hide');
$('#collapseOnePanel').collapse('show');
}else if(this.id == 'collapseTwo'){
$('#collapseOnePanel').collapse('hide');
$('#collapseThreePanel').collapse('hide');
$('#collapseTwoPanel').collapse('show');
}else if(this.id == 'collapseThree'){
$('#collapseOnePanel').collapse('hide');
$('#collapseTwoPanel').collapse('hide');
$('#collapseThreePanel').collapse('show');
}
})
通过这种方式,您可以自己控制崩溃事件。 希望这会有所帮助。