也可以通过css样式来实现。
在CSS中添加样式:
.text-toggle[aria-expanded=false] .text-expanded {
display: none;
}
.text-toggle[aria-expanded=true] .text-collapsed {
display: none;
}
并在html中使用:
<a class="btn btn-default text-toggle" data-toggle="collapse" data-target="#tabsNavigation" aria-expanded="false">
<span class="text-collapsed">More info</span>
<span class="text-expanded">Less info</span>
<i class="fa fa-angle-right"></i>
</a>
请记得添加属性
aria-expanded="false"
和班级
text-toggle
链接/按钮。
基于:https://stackoverflow.com/a/16870379/1596547
$('button span').parent().click(function () {
if($('button span').hasClass('glyphicon-chevron-down'))
{
$('button').html('<span class="glyphicon glyphicon-chevron-up"></span> Close');
}
else
{
$('button').html('<span class="glyphicon glyphicon-chevron-down"></span> Open');
}
});
如果您使用 jQuery,请使用
toggleClass
$('div.toggler').click(function(){
$('div.toggler span').toggleClass('glyphicon-chevron-down glyphicon-chevron-up');
});
在这里工作 JSFiddle
$('#more').click(function () {
if($('button span').hasClass('glyphicon-chevron-down'))
{
$('#more').html('<span class="glyphicon glyphicon-chevron-up"></span> Less Info');
}
else
{
$('#more').html('<span class="glyphicon glyphicon-chevron-down"></span> More Info');
}
});
$(document).ready(function(){
$("#demo").on("hide.bs.collapse", function(){
$(".btn").html('<span class="glyphicon glyphicon-collapse-down"></span> Open');
});
$("#demo").on("show.bs.collapse", function(){
$(".btn").html('<span class="glyphicon glyphicon-collapse-up"></span> Close');
});
$("#demo1").on("hide.bs.collapse", function(){
$(".btn1").html('<span class="glyphicon glyphicon-collapse-down"></span> Open');
});
$("#demo1").on("show.bs.collapse", function(){
$(".btn1").html('<span class="glyphicon glyphicon-collapse-up"></span> Close');
});
});