简体   繁体   English

Bootstrap折叠文字更改

[英]Bootstrap collapse text change

i'm using bootstrap collapse. 我正在使用bootstrap崩溃。 I'm trying to add a simple jquery statement, once a user click on one of the collapse than a icon changes. 我试图添加一个简单的jquery语句,一旦用户单击其中一个折叠而不是图标更改。 I have multiple collapses on the page so, I'm guessing I'll have to use the "this" statement in the code. 我在页面上有多个折叠,因此,我猜我将不得不在代码中使用“ this”语句。

Here is the HTML 这是HTML

<a data-toggle="collapse" href="#y2012" aria-expanded="false" aria-controls="y2012">
  <i class="fa fa-caret-right"></i> 2012
</a><br />
<div class="collapse" id="y2012">
  text
</div>
<a data-toggle="collapse" href="#y2013" aria-expanded="false" aria-controls="y2013">
  <i class="fa fa-caret-right"></i> 2013
</a><br />
<div class="collapse" id="y2013">
  text
</div>

Here is my JS i got at the moment 这是我现在得到的我的JS

$(".collapse").hasClass("in")?true : (function(){
    $("a i").removeClass('fa-caret-right');
    $("a i").addClass('fa-caret-down');
}, function () {
    $("a i").removeClass('fa-caret-down');
    $("a i").addClass('fa-caret-right');
});

---------------UPDATED---------------- So I've kind of got it working I've changed the js to --------------- UPDATED ----------------所以我已经将其更改为

    $('.collapsible').click(function () {
        var $this = $(this);
        $this.toggleClass('collapsible');
        if ($this.hasClass('collapsible')) {
            $this.children("i").removeClass('fa-caret-right');
            $this.children("i").addClass('fa-caret-down');
        } else {
            $this.children("i").addClass('fa-caret-right');
            $this.children("i").removeClass('fa-caret-down');
        }
    });

And have added the class collapsible to the a tag so it would be 并且将可折叠类添加到了标签中,因此

  <a data-toggle="collapse" href="#y2012" aria-expanded="false" aria-controls="y2012" class="collapsible">
    <i class="fa fa-caret-right"></i> 2012
  </a><br />

It now kind of works but the issue is once you click the a tag at first it does nothing, then click it again the function starts working, which then becomes unsync to the way i wanted it to work in the first place. 现在它可以工作了,但问题是,一旦您首先单击一个标签,它什么都不做,然后再次单击它,该功能开始起作用,然后与我希望它首先起作用的方式不同步。

$(".collapse").click(function(){
  $(this).hasClass("in")?true : (function(){
      $(this).children("i").removeClass('fa-caret-right');
      $(this).children("i").addClass('fa-caret-down');
  }, function () {
      $(this).children("i").removeClass('fa-caret-down');
      $(this).children("i").addClass('fa-caret-right');
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM