jquery 如何修改 Bootstrap 中的箭头?

daupos2t  于 2022-12-18  发布在  jQuery
关注(0)|答案(4)|浏览(178)

我写了一些JavaScript,并试图在aria-expanded元素为true或false时替换类,但似乎什么也没发生。当页面加载时,第一张卡片是打开的,因为aria-expanded =“true”。我想找到一种方法,在aria-expanded元素的值发生变化时替换类。我是jQuery和webdev的新手,因此感谢您的所有帮助。我使用了 Bootstrap accordion 来确保一次只能打开一个

<div id="headingOne">
    <h5 class="card-title">
        Or Files
        <a data-toggle="collapse" class="float-right" href="#collapseFiles" 
          role="button" aria-expanded="true" data-target="#collapseFiles" 
          aria-controls="collapseNotes">
            <i class="fas fa-angle-up></i>
        </a>
    </h5>
</div>
<div id="headingTwo">
             <h5 class="card-title">
             Or Notes
                <a data-toggle="collapse" class="float-right" href="#collapseNotes" role="button" aria-expanded="false" data-target="#collapseNotes" aria-controls="collapseNotes">
                        <i class="fas fa-angle-down"></i>
                </a>
              </h5>
  </div>
<div id="headingThree">
             <h5 class="card-title">
             Or Names
                <a data-toggle="collapse" class="float-right" href="#collapseNames" role="button" aria-expanded="false" data-target="#collapseNames" aria-controls="collapseNotes">
                        <i class="fas fa-angle-down"></i>
                </a>
              </h5>
  </div>

这就是jquery。

if ($('a').attr('aria-expanded').val() == 'true') {
    $("a i").attr("class", "fa-angle-down");
} else {
    $("a i").attr("class", "fa-angle-up");
}
dw1jzc5e

dw1jzc5e1#

我知道您可能希望使用JavaScript来完成此操作,但我有一种纯CSS方法来完成此操作:

HTML

我删除了锚标记上所有不必要的属性。我也删除了图标上的fa-angle-down。图标状态将通过CSS设置。

<div id="headingOne">
    <h5 class="card-title">
        Or Files
        <a data-toggle="collapse" class="float-right" data-target="#collapseFiles">
            <i class="fas"></i>
        </a>
    </h5>
    <div class="card-body">
        <p id="collapseFiles" class="card-text collapse show">
            Some quick example text to build on the card title and make up the bulk of 
            the card's content.
        </p>
    </div>
</div>
<div id="headingTwo">
    <h5 class="card-title">
        Or Notes
        <a data-toggle="collapse" class="float-right" data-target="#collapseNotes">
            <i class="fas"></i>
        </a>
    </h5>
    <div class="card-body">
        <p id="collapseNotes" class="card-text collapse show">
            Some quick example text to build on the card title and make up the bulk of 
            the card's content.
        </p>
    </div>
</div>
<div id="headingThree">
    <h5 class="card-title">
        Or Name
        <a data-toggle="collapse" class="float-right" data-target="#collapseNames">
            <i class="fas"></i>
        </a>
    </h5>
    <div  class="card-body">
        <p id="collapseNames" class="card-text collapse show">
            Some quick example text to build on the card title and make up the bulk of 
            the card's content.
        </p>
    </div>
</div>

CSS

您可以编写CSS来定位任何具有data-toggle="collapse"属性的锚标记。当它折叠时,Bootstrap会添加.collapsed类,以便您可以使用它来相应地设置图标的样式。

a[data-toggle="collapse"] i.fas:before {
    content: "\f107";    /* angle-down */
}

a[data-toggle="collapse"].collapsed i.fas:before {
    content: "\f106";    /* angle-up */
}

屏幕截图

小提琴演示

https://jsfiddle.net/davidliang2008/og1t8ksj/26/

p1tboqfb

p1tboqfb2#

您需要使用事件来捕获更改,例如https://api.jquery.com/on/

kpbwa7wx

kpbwa7wx3#

试试这个:-

$('[data-toggle="collapse"]').on('click', function(){
    if ($(this).attr('aria-expanded') == 'true') {
        $(this).find('i').addClass('fa-angle-up').removeClass('fa-angle-down');
    } else {
        $(this).find('i').addClass('fa-angle-down').removeClass('fa-angle-up');
    }
});
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel='stylesheet'>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" ></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>

<div id="headingTwo">
    <h5 class="card-title">
        Or Notes
        <a data-toggle="collapse" class="float-right" href="#collapseNotes" role="button" aria-expanded="false" data-target="#collapseNotes" aria-controls="collapseNotes">
            <i class="fa fa-angle-up"></i>
        </a>
    </h5>
</div>
<div class="collapse" id="collapseNotes">
    Anim pariatur cliche reprehenderit
</div>
slwdgvem

slwdgvem4#

已经有一段时间了,但我想这可能是这个问题的一个很好的答案:不需要使用JAVASCRIPT来完成这个任务。利用类'collapted',当折叠折叠时,它被添加到 Package 'a'中。

<a data-toggle="collapse" class="float-right" href="#collapseFiles" 
      role="button" aria-expanded="true" data-target="#collapseFiles" 
      aria-controls="collapseNotes">
  <span class="arrowup"> <i class="fas fa-angle-up></i> </span>
  <span class="arrowdown"> <i class="fas fa-angle-dowm></i> </span>
</a>

然后在样式表上:
.折叠.向下箭头{显示:无} ---------- .折叠式.折叠.向上箭头{显示:无} --------- .折叠式.折叠.向下箭头{显示:块}

相关问题