Bootstrap 崩溃:更改切换按钮图标和文本的显示

fkvaft9z  于 2022-12-07  发布在  Bootstrap
关注(0)|答案(5)|浏览(188)

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


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

稍后,我需要更改文本、按钮图标并展开。

是否打开:

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

已关闭:

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

llycmphe1#

也可以通过css样式来实现。
在css中添加样式:

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

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

请记住添加属性

aria-expanded="false"

和类

text-toogle

链接/按钮。

kmb7vmvb

kmb7vmvb2#

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

mitkmikd3#

如果使用jQuery,则使用toggleClass

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

在这里工作JSFiddle

mrzz3bfm

mrzz3bfm4#

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

z5btuh9x

z5btuh9x5#

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

相关问题