引导 4:在前循环中将一个图标与另一个图标交换的最佳方式是什么?

我想在使用折叠时交换,在使用foreach循环时没有每个图标同时切换。fa fa-plusfa fa-minus


这是我的代码演示。


<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" />

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />

<script src="https://code.jquery.com/jquery-3.4.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.4.1/js/bootstrap.min.js"></script>


<div class="container h-100">


    <div class="row no-gutters border-0 rounded align-items-center mt-3">

        <div class="col-sm-4">demo 1</div>

        <div class="col-sm-4">demo 1</div>

        <div class="col-sm-3">demo 1</div>

        <div class="col-sm-1 ">

            <a class="fa fa-plus" data-toggle="collapse" href="#collapse1" aria-expanded="true" aria-controls="collapse1"></a>

        </div>

    </div><!--row-->


    <div class="collapse" id="collapse1">

        <div class="row no-gutters border-0 rounded align-items-center mt-3">

            <div class="col-sm-4">demo 1</div>

            <div class="col-sm-4">demo 1</div>

            <div class="col-sm-4">demo 1</div>

        </div><!--collapse-->

    </div>


    <div class="row no-gutters border-0 rounded align-items-center mt-3">

        <div class="col-sm-4">demo 2</div>

        <div class="col-sm-4">demo 2</div>

        <div class="col-sm-3">demo 2</div>

        <div class="col-sm-1 ">

            <a class="fa fa-plus" data-toggle="collapse" href="#collapse2" aria-expanded="true" aria-controls="collapse2"></a>

        </div>

    </div><!--row-->

https://codepen.io/benwhittaker25/pen/rNVWyby

在引导 4.4 中实现此目的的最佳方法是什么?


30秒到达战场
浏览 81回答 2
2回答

红颜莎娜

您可以在单击按钮时使用事件 Bootstrap 触发器来执行此操作:$('.collapse').on('show.bs.collapse hide.bs.collapse', function () {&nbsp; // Find the corresponding button&nbsp; var $btn = $('[data-toggle="collapse"][href="#' + this.id + '"]');&nbsp; // Find the corresponding row&nbsp; var $row = $btn.closest('.row');&nbsp;&nbsp;&nbsp; // Toggle classes&nbsp; $btn.toggleClass('fa-minus fa-plus');&nbsp; $row.toggleClass('rounded rounded-top');});<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" /><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" /><script src="https://code.jquery.com/jquery-3.4.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.4.1/js/bootstrap.min.js"></script><div class="container h-100">&nbsp; &nbsp; <div class="row no-gutters border-0 rounded align-items-center mt-3">&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 1</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 1</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-3">demo 1</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-1 ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class="fa fa-plus" data-toggle="collapse" href="#collapse1" aria-expanded="true" aria-controls="collapse1"></a>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div><!--row-->&nbsp; &nbsp; <div class="collapse" id="collapse1">&nbsp; &nbsp; &nbsp; &nbsp; <div class="row no-gutters border-0 rounded align-items-center mt-3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 1</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 1</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 1</div>&nbsp; &nbsp; &nbsp; &nbsp; </div><!--collapse-->&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="row no-gutters border-0 rounded align-items-center mt-3">&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 2</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 2</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-3">demo 2</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-1 ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class="fa fa-plus" data-toggle="collapse" href="#collapse2" aria-expanded="true" aria-controls="collapse2"></a>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div><!--row-->&nbsp; &nbsp; <div class="collapse" id="collapse2">&nbsp; &nbsp; &nbsp; &nbsp; <div class="row no-gutters border-0 rounded align-items-center mt-3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 2</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 2</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 2</div>&nbsp; &nbsp; &nbsp; &nbsp; </div><!--collapse-->&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="row no-gutters border-0 rounded align-items-center mt-3">&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 3</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 3</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-3">demo 3</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-1 ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class="fa fa-plus" data-toggle="collapse" href="#collapse3" aria-expanded="true" aria-controls="collapse3"></a>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div><!--row-->&nbsp; &nbsp; <div class="collapse" id="collapse3">&nbsp; &nbsp; &nbsp; &nbsp; <div class="row no-gutters border-0 rounded align-items-center mt-3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 3</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 3</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 3</div>&nbsp; &nbsp; &nbsp; &nbsp; </div><!--collapse-->&nbsp; &nbsp; </div></div><!--container-->

慕标琳琳

优雅的方法是做blex所展示的jquery方式。但是你也可以绑定一个点击函数,就像下面的javascript方式一样:function toggleClass(item){&nbsp; &nbsp; if(item.classList.contains('fa-plus')){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item.classList.remove('fa-plus');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item.classList.add('fa-minus');&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; item.classList.remove('fa-minus');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; item.classList.add('fa-plus');&nbsp; &nbsp; }}<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" /><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" /><script src="https://code.jquery.com/jquery-3.4.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.4.1/js/bootstrap.min.js"></script><div class="container h-100"><div class="container h-100">&nbsp; &nbsp; <div class="row no-gutters border-0 rounded align-items-center mt-3">&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 1</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 1</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-3">demo 1</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-1 ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class="fa fa-plus" data-toggle="collapse" href="#collapse1" aria-expanded="true" aria-controls="collapse1" onClick="toggleClass(this)"></a>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div><!--row-->&nbsp; &nbsp; <div class="collapse" id="collapse1">&nbsp; &nbsp; &nbsp; &nbsp; <div class="row no-gutters border-0 rounded align-items-center mt-3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 1</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 1</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 1</div>&nbsp; &nbsp; &nbsp; &nbsp; </div><!--collapse-->&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="row no-gutters border-0 rounded align-items-center mt-3">&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 2</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 2</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-3">demo 2</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-1 ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class="fa fa-plus" data-toggle="collapse" href="#collapse2" aria-expanded="true" aria-controls="collapse2" onClick="toggleClass(this)"></a>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div><!--row-->&nbsp; &nbsp; <div class="collapse" id="collapse2">&nbsp; &nbsp; &nbsp; &nbsp; <div class="row no-gutters border-0 rounded align-items-center mt-3">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 2</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 2</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 2</div>&nbsp; &nbsp; &nbsp; &nbsp; </div><!--collapse-->&nbsp; &nbsp; </div>&nbsp; &nbsp; <div class="row no-gutters border-0 rounded align-items-center mt-3">&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 3</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 3</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-3">demo 3</div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-1 ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a class="fa fa-plus" data-toggle="collapse" href="#collapse3" aria-expanded="true" aria-controls="collapse3" onClick="toggleClass(this)"></a>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div><!--row-->&nbsp; &nbsp; <div class="collapse" id="collapse3">&nbsp; &nbsp; &nbsp; &nbsp; <div class="row no-gutters border-0 rounded align-items-center mt-3" >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 3</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 3</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-sm-4">demo 3</div>&nbsp; &nbsp; &nbsp; &nbsp; </div><!--collapse-->&nbsp; &nbsp; </div></div><!--container-->
打开App,查看更多内容
随时随地看视频慕课网APP