我正在使用Bootstrap的collapse.js在Rails应用程序中扩展和折叠一些输入组。我使用JS确定组是否已展开,并创建了CSS类来添加“+”或“-”,以显示它是打开还是关闭状态。
打开:
关闭:
正如您从CSS中所看到的那样,我正在使用一个png背景图像,该图像位于我的图像文件夹中。
.expandable {
background: url('/assets/plus.png');
padding-top: 4px;
width: 400px;
height: 30px;
background-repeat: no-repeat;
padding-left: 55px;
display: block;
}
.expanded {
background: url('/assets/minus.png');
padding-top: 4px;
width: 400px;
height: 30px;
background-repeat: no-repeat;
padding-left: 55px;
display: block;
}
我希望使用glyphicon-plus和glyphicon-minus代替这些.png文件。
实现这一目标的最佳方式是什么?
更新:
为了获得正确的样式,我更改了CSS为:
.expandable {
height:40px;
width:50%;
margin:6px;
}
.expandable:before{
content:"\2b";
font-family:"Glyphicons Halflings";
line-height:1;
margin:5px;
}
.expanded {
height:40px;
width:50%;
margin:6px;
}
.expanded:before{
content:"\2212";
font-family:"Glyphicons Halflings";
line-height:1;
margin:5px;
}
参考我的HTML代码:
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<p1 class="panel-title" >
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" class="expandable">
Provider details
</a>
<p2>
</div>
<div id="collapseOne" class="panel-collapse collapse">
<div class="panel-body">
blah, blah....
使用JS来检测部分内容的展开/折叠状态:
$ ->
$(".expandable").click () ->
$this = $(this)
if $this.hasClass("expandable")
$this.removeClass("expandable").addClass "expanded"
else $this.removeClass("expanded").addClass "expandable" if $this.hasClass("expanded")
return
return