bootstrap collapse: change display of toggle button icons and text

旧街凉风 提交于 2020-01-01 04:47:07

问题


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>

回答1:


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



回答2:


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:


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:


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:


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


来源:https://stackoverflow.com/questions/19050972/bootstrap-collapse-change-display-of-toggle-button-icons-and-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!