bootstrap collapse: change display of toggle button icons and text

后端 未结 5 1027
[愿得一人]
[愿得一人] 2021-01-01 23:15

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

相关标签:
5条回答
  • 2021-01-01 23:21

    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/

    0 讨论(0)
  • 2021-01-01 23:26

    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.

    0 讨论(0)
  • 2021-01-01 23: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'); 
    }
    }); 
    
    0 讨论(0)
  • 2021-01-01 23: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');
        });
    });
    
    0 讨论(0)
  • 2021-01-01 23:36

    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

    0 讨论(0)
提交回复
热议问题