Twitter bootstrap collapse: change display of toggle button

后端 未结 10 691
有刺的猬
有刺的猬 2020-12-12 23:50

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:

<         


        
相关标签:
10条回答
  • 2020-12-13 00:19

    Add some jquery code, you need jquery to do this :

    <script>
            $(".btn[data-toggle='collapse']").click(function() {
                if ($(this).text() == '+') {
                    $(this).text('-');
                } else {
                    $(this).text('+');
                }
            });
            </script>
    
    0 讨论(0)
  • 2020-12-13 00:21

    try this. http://jsfiddle.net/fVpkm/

    Html:-

    <div class="row-fluid summary">
        <div class="span11">
            <h2>MyHeading</h2>  
        </div>
        <div class="span1">
            <button class="btn btn-success" data-toggle="collapse" data-target="#intro">+</button>
        </div>
    </div>
    <div class="row-fluid summary">
        <div id="intro" class="collapse"> 
            Here comes the text...
        </div>
    </div>
    

    JS:-

    $('button').click(function(){ //you can give id or class name here for $('button')
        $(this).text(function(i,old){
            return old=='+' ?  '-' : '+';
        });
    });
    

    Update With pure Css, pseudo elements

    http://jsfiddle.net/r4Bdz/

    Supported Browsers

    button.btn.collapsed:before
    {
        content:'+' ;
        display:block;
        width:15px;
    }
    button.btn:before
    {
        content:'-' ;
        display:block;
        width:15px;
    }
    

    Update 2 With pure Javascript

    http://jsfiddle.net/WteTy/

    function handleClick()
    {
        this.value = (this.value == '+' ? '-' : '+');
    }
    document.getElementById('collapsible').onclick=handleClick;
    
    0 讨论(0)
  • 2020-12-13 00:22

    All the other solutions posted here cause the toggle to get out of sync if it is double clicked. The following solution uses the events provided by the Bootstrap framework, and the toggle always matches the state of the collapsible element:

    HTML:

    <div class="row-fluid summary">
        <div class="span11">
            <h2>MyHeading</h2>  
        </div>
        <div class="span1">
            <button id="intro-switch" class="btn btn-success" data-toggle="collapse" data-target="#intro">+</button>
        </div>
    </div>
    <div class="row-fluid summary">
        <div id="intro" class="collapse"> 
            Here comes the text...
        </div>
    </div>
    

    JS:

    $('#intro').on('show', function() {
      $('#intro-switch').html('-')
    })
    $('#intro').on('hide', function() {
      $('#intro-switch').html('+')
    })
    

    That should work for most cases.

    However, I also ran into an additional problem when trying to nest one collapsible element and its toggle switch inside another collapsible element. With the above code, when I click the nested toggle to hide the nested collapsible element, the toggle for the parent element also changes. It may be a bug in Bootstrap. I found a solution that seems to work: I added a "collapsed" class to the toggle switches (Bootstrap adds this when the collapsible element is hidden but they don't start out with it), then added that to the jQuery selector for the hide function:

    http://jsfiddle.net/fVpkm/87/

    HTML:

    <div class="row-fluid summary">
        <div class="span11">
            <h2>MyHeading</h2>  
        </div>
        <div class="span1">
            <button id="intro-switch" class="btn btn-success collapsed" data-toggle="collapse" data-target="#intro">+</button>
        </div>
    </div>
    <div class="row-fluid summary">
        <div id="intro" class="collapse"> 
            Here comes the text...<br>
            <a id="details-switch" class="collapsed" data-toggle="collapse" href="#details">Show details</a>
            <div id="details" class="collapse">
                More details...
            </div>
        </div>
    </div>
    

    JS:

    $('#intro').on('show', function() {
        $('#intro-switch').html('-')
    })
    $('#intro').on('hide', function() {
        $('#intro-switch.collapsed').html('+')
    })
    
    $('#details').on('show', function() {
        $('#details-switch').html('Hide details')
    })
    $('#details').on('hide', function() {
        $('#details-switch.collapsed').html('Show details')
    })
    
    0 讨论(0)
  • 2020-12-13 00:22

    You do like this. the function return the old text.

    $('button').click(function(){ 
            $(this).text(function(i,old){
                return old=='Read More' ?  'Read Less' : 'Read More';
            });
        });
    
    0 讨论(0)
提交回复
热议问题