引导折叠:更改切换按钮图标和文本的显示

问题描述 投票:0回答:5
twitter-bootstrap twitter-bootstrap-3
5个回答
54
投票

也可以通过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

链接/按钮。


8
投票

基于: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'); 
}
}); 

3
投票

如果您使用 jQuery,请使用

toggleClass

$('div.toggler').click(function(){
  $('div.toggler span').toggleClass('glyphicon-chevron-down glyphicon-chevron-up');
});

在这里工作 JSFiddle


0
投票
我发现这个问题和答案很有帮助。我删除了parent()方法,并向我的+按钮添加了一个id,以避免在切换+时更改其他按钮。

$('#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'); } });

http://jsfiddle.net/maybolles/opbyvbv4/2/


-1
投票
使用此 JavaScript 来更改图标

$(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'); }); });
    
© www.soinside.com 2019 - 2024. All rights reserved.