使用Bootstrap收合类别不适用于多个div标签

时间:2019-02-06 19:26:56

标签: javascript jquery html twitter-bootstrap

我有一个项目A,B,C,D,E的列表。您在这些列表项目之一上单击的每个项目都有一个引导类“ collapse”,其中显示了一个带文本的div。一切正常。但是,您必须再次单击列表项以折叠包含文本的div。

我试图确保在单击列表项时,所有其他openend div立即关闭。

我找到了一些提到要使用“数据父级”的内容,但是如下面的代码片段所示,这是行不通的。要查看问题,请查看代码段并展开代码段。

要复制我的问题,请执行以下步骤:

  1. 点击项目A(您会在下面看到文字“ A”)
  2. 点击项目B(您会看到文本“ A”和“ B”一样出现)
  3. 点击项目C(您将看到文本“ A”,“ B” C”)
  4. 点击项目A(您现在将看到“ B”,“ C”),但A现在已经合拢

我试图确保您只看到与您单击的div的ID有关的文本。

SPOpen(1,9600,1,0,8)
SPOpen( "COM1" )
SPSendString(1, "*IDN?" )
string message 
number test
message = SPReceiveString(1,8,test)
Result("Acquisition "+message+" "+test+"\n")
SPClose(1)

1 个答案:

答案 0 :(得分:1)

您可以使用类似jquery的

<script type="text/javascript">
  $(document).ready(function(){
    $('[data-toggle="collapse"]').click(function(event) {
        if ($('.collapse.in').length > 0) {
            $('.collapse.in').collapse('hide')
        }
    });
  })
</script>

data-toggle="collapse"单击时,意味着在列表项上单击,所有打开的div(带有.in类)都被collapse function关闭。

<!DOCTYPE html>
<html>
    <head>
        <meta content="width=device-width, initial-scale=1" name="viewport">
            <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css" rel="stylesheet">
                <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
                </script>
                <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js">
                </script>
            </link>
        </meta>
    </head>
    <body>
        <!-- Start Restaurant Menu -->
        <section id="mu-restaurant-menu">
            <div class="container">
                <div id="menuGroup">
                    <div class="panel">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="mu-restaurant-menu-area">
                                    <div class="mu-title">
                                        <p>
                                        </p>
                                        <h2>
                                            LIST ITEM
                                        </h2>
                                    </div>
                                    <div class="row">
                                        <div class="col-md-12">
                                            <div class="mu-counter-area">
                                                <ul class="mu-counter-nav">
                                                    <li class="col-md-2 col-sm-3 col-xs-12">
                                                        <div aria-controls="collapseFood" aria-expanded="false" class="mu-single-counter" data-parent="#menuGroup" data-toggle="collapse" href="#collapseA">
                                                            <span>
                                                                A
                                                            </span>
                                                        </div>
                                                    </li>
                                                    <li class="col-md-2 col-sm-3 col-xs-12">
                                                        <div aria-controls="collapseVegan" aria-expanded="false" class="mu-single-counter" data-parent="#menuGroup" data-toggle="collapse" href="#collapseB">
                                                            <span>
                                                                B
                                                            </span>
                                                        </div>
                                                    </li>
                                                    <li class="col-md-2 col-sm-3 col-xs-12">
                                                        <div aria-controls="collapseJain" aria-expanded="false" class="mu-single-counter" data-parent="#menuGroup" data-toggle="collapse" href="#collapseC">
                                                            <span>
                                                                C
                                                            </span>
                                                        </div>
                                                    </li>
                                                    <li class="col-md-2 col-sm-6 col-xs-12">
                                                        <div aria-controls="collapseLunch" aria-expanded="false" class="mu-single-counter" data-parent="#menuGroup" data-toggle="collapse" href="#collapseD">
                                                            <span>
                                                                D
                                                            </span>
                                                        </div>
                                                    </li>
                                                    <li class="col-md-2 col-sm-3 col-xs-12">
                                                        <div aria-controls="collapseCocktail" aria-expanded="false" class="mu-single-counter" data-parent="#menuGroup" data-toggle="collapse" href="#collapseE">
                                                            <span>
                                                                E
                                                            </span>
                                                        </div>
                                                    </li>
                                                </ul>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="mu-restaurant-menu-content">
                                        <div class="row visible-lg visible-md visible-sm visible-xs collapsable-content">
                                            <div class="container">
                                                <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                                                    <div class="main-menu-box">
                                                        <hr>
                                                            <div class="collapse" id="collapseA">
                                                                <div class="intro">
                                                                    A
                                                                </div>
                                                                <hr>
                                                                </hr>
                                                            </div>
                                                            <div aria-expanded="true" class="collapse" id="collapseB">
                                                                <div class="intro">
                                                                    B
                                                                </div>
                                                                <hr>
                                                                </hr>
                                                            </div>
                                                            <div aria-expanded="true" class="collapse" id="collapseC">
                                                                <div class="intro">
                                                                    C
                                                                </div>
                                                                <hr>
                                                                </hr>
                                                            </div>
                                                            <div aria-expanded="true" class="collapse" id="collapseD">
                                                                <div class="intro">
                                                                    D
                                                                </div>
                                                                <hr>
                                                                </hr>
                                                            </div>
                                                            <div aria-expanded="true" class="collapse" id="collapseE">
                                                                <div class="intro">
                                                                    E
                                                                </div>
                                                                <hr>
                                                                </hr>
                                                            </div>
                                                        </hr>
                                                    </div>
                                                </div>
                                            </div>
                                            <hr>
                                            </hr>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </body>
</html>
<!-- End Restaurant Menu -->
<script type="text/javascript">
$(document).ready(function(){
    $('[data-toggle="collapse"]').click(function(event) {
        if ($('.collapse.in').length > 0) {
            $('.collapse.in').collapse('hide')
        }
    });
})
</script>