twitter-bootstrap bootstrap 折叠:更改切换按钮图标和文本的显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19050972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 18:37:28  来源:igfitidea点击:

bootstrap collapse: change display of toggle button icons and text

twitter-bootstraptwitter-bootstrap-3collapse

提问by user2823276

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:

我正在使用 Twitter Bootstrap 创建可折叠的文本部分。当按下 + 按钮时,这些部分会展开。我的html代码如下:

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>

采纳答案by Bass Jobsen

based on: https://stackoverflow.com/a/16870379/1596547

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

回答by Pawe?

It is also possible to do by css styles.

也可以通过 css 样式来实现。

in css add style:

在css中添加样式:

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

and use in html:

并在 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.

链接/按钮。

回答by May Allen

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 +.

我发现这个问题和答案很有帮助。我删除了 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/

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

回答by prgmrDev

If you're using jQuery then make use of toggleClass

如果您使用的是 jQuery,那么请使用 toggleClass

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

Working JSFiddlehere

在这里工作JSFiddle

回答by RASA

use this javascript for changing icon

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