代码之家  ›  专栏  ›  技术社区  ›  RA19

使用引导折叠类不适用于多个DIV标记

  •  0
  • RA19  · 技术社区  · 6 年前

    我有一个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相关的文本。

    <!DOCTYPE html>
    <html>
    <head>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
      <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>
    </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>&nbsp;</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 class="mu-single-counter" data-toggle="collapse" href="#collapseA" aria-expanded="false" data-parent="#menuGroup" aria-controls="collapseFood">
                                              <span>A</span>
                                           </div>
                                        </li>
                                        <li class="col-md-2 col-sm-3 col-xs-12">
                                           <div class="mu-single-counter" data-toggle="collapse" href="#collapseB" aria-expanded="false" data-parent="#menuGroup" aria-controls="collapseVegan">
                                              <span>B</span>
                                           </div>
                                        </li>
                                        <li class="col-md-2 col-sm-3 col-xs-12">
                                           <div class="mu-single-counter" data-toggle="collapse"   href="#collapseC" aria-expanded="false" data-parent="#menuGroup" aria-controls="collapseJain">
                                              <span>C</span>
                                           </div>
                                        </li>
                                        <li class="col-md-2 col-sm-6 col-xs-12">
                                           <div class="mu-single-counter" data-toggle="collapse" href="#collapseD" aria-expanded="false" data-parent="#menuGroup" aria-controls="collapseLunch">
                                              <span>D</span>
                                           </div>
                                        </li>
                                     
                                        <li class="col-md-2 col-sm-3 col-xs-12">
                                           <div class="mu-single-counter" data-toggle="collapse" href="#collapseE" aria-expanded="false" data-parent="#menuGroup" aria-controls="collapseCocktail">
                                              <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" aria-expanded="true" >
                                              <div class="intro">
    										  A
                                                 </div>
                                              <hr>
                                           </div>
    
    													<div class="collapse" id="collapseB" aria-expanded="true" >
                                              <div class="intro">
    										  B
                                                 </div>
                                              <hr>
                                           </div>
                                          
                                          						<div class="collapse" id="collapseC" aria-expanded="true" >
                                              <div class="intro">
    										  C
                                                 </div>
                                              <hr>
                                           </div>
                                          
                                          			<div class="collapse" id="collapseD" aria-expanded="true" >
                                              <div class="intro">
    										  D
                                                 </div>
                                              <hr>
                                           </div>
    
                                          
                                               
                                          			<div class="collapse" id="collapseE" aria-expanded="true" >
                                              <div class="intro">
    										  E
                                                 </div>
                                              <hr>
                                           </div>
      
    
    
    
    
    
                                                          </div>
                                                       </div>
                                                    </div>
                                                    <hr>
                                                 </div>
                                              </div>
    
    
    
    
    
    
                                     </div>
                                  </div>
                               </div>
                            </div>
                         </div>
                      </div>
                   </div>
                       </div>
                     </div>
                     </div>
                    </div>
             </section>
             <!-- End Restaurant Menu -->
        
    </body>
    </html>
    1 回复  |  直到 6 年前
        1
  •  1
  •   weegee Chinnon Santos    6 年前

    您可以使用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" 单击,意味着在列表项上单击,打开的所有分区(在类中与它们一起)都与 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>