bootstrap collapse: change display of toggle butto

2019-03-26 00:13发布

I am using Twitter Bootstrap to create collapsible sections of text. The sections are expanded when a + button is pressed. My html code as follows:

Demo link

<div class="panel-group" id="accordion">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#demo">
          <span class="glyphicon glyphicon-chevron-down"></span> Open
        </button>
      </h4>
    </div>
    <div id="demo" class="panel-collapse collapse in">
      <div class="panel-body">
        Contents:Thank you to help me solve the problem, you're a great guy!
      </div>
    </div>
  </div>
</div>

Later, i need to change text, button icons and expand.

Is Open:

<button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#demo">
  <span class="glyphicon glyphicon-chevron-up"></span> Close
</button>

Is Close:

<button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#demo">
  <span class="glyphicon glyphicon-chevron-down"></span> Open
</button>

5条回答
女痞
2楼-- · 2019-03-26 00:19

It is also possible to do by css styles.

in css add style:

.text-toogle[aria-expanded=false] .text-expanded {
  display: none;
}
.text-toogle[aria-expanded=true] .text-collapsed {
  display: none;
}

and use in html:

<a class="btn btn-default text-toogle" 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>

Please remember to add attribute

aria-expanded="false"

and class

text-toogle

to link / button.

查看更多
放荡不羁爱自由
3楼-- · 2019-03-26 00:22

I found this question and answer helpful. I removed the parent() method and added an id to my + button to avoid changing other buttons when toggling +.

$('#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/

查看更多
欢心
4楼-- · 2019-03-26 00:29

If you're using jQuery then make use of toggleClass

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

Working JSFiddle here

查看更多
我只想做你的唯一
5楼-- · 2019-03-26 00:32

based on: 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'); 
}
}); 
查看更多
放我归山
6楼-- · 2019-03-26 00:33

use this javascript for changing icon

$(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');
    });
});
查看更多
登录 后发表回答