简体   繁体   English

[Bootstrap]-将Dropdown与Collapse一起使用时的奇怪行为

[英][Bootstrap]- Odd behavior when Dropdown used with Collapse

I am fairly new with the development using Bootstrap and I have been trying to create a dropdown which has the feature of collapsible such that any sub item with the dropdown can be shown within the collapsible container. 对于使用Bootstrap进行的开发,我是一个新手,我一直在尝试创建一个具有可折叠功能的下拉菜单,以便具有该下拉菜单的任何子项都可以显示在可折叠容器中。

I found some code online which pretty much explains what i am trying to accomplish: https://www.bootply.com/1u6VW4bsrR 我在网上找到了一些代码,这些代码几乎可以解释我要完成的工作: https : //www.bootply.com/1u6VW4bsrR

I tried to write down few lines of HTML code to accomplish similar to above: 我试图写下几行HTML代码来完成与上述类似的操作:

<div class="dropdown">
  <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Dropdown Example
  <span class="caret"></span></button>
  <ul class="dropdown-menu">
     <li>
       <a data-toggle="collapse" href="#collapse1">HTML</a>
       <div id="collapse1" class="panel-collapse collapse">
         <div class="panel-body">
           <ul class='dropdown-menu sub-menu'>
             <li><a>Type 1</a></li>
             <li><a>Type 2</a></li>
           </ul>
         </div>
       </div>
    </li>
    <li><a href="#">CSS</a></li>
    <li><a href="#">JavaScript</a></li>
 </ul>
</div>

Codepen link: https://codepen.io/anon/pen/weLKzp/?editors=1010 Codepen链接: https ://codepen.io/anon/pen/weLKzp/ editors = 1010

But it is giving me hard time. 但这给我带来了困难。 There are two specific behavior that i noticed: 我注意到了两种特定的行为:

  1. When i click on dropdown, and following after click on collapsible div(HTML), then the dropdown disappears. 当我单击下拉菜单时,然后单击可折叠div(HTML)之后,下拉菜单将消失。 When clicked back on the dropdown, it is getting shown. 当再次单击下拉菜单时,它会显示出来。
  2. When the dropdown is shown having collapsible div, all the sub-items with the collapsible panel body doesn't appear. 当显示下拉列表具有可折叠的div时,不会出现带有可折叠面板主体的所有子项目。

Can you help me figuring out where are the changes required and what i am doing wrong? 您能帮我弄清楚需要进行哪些更改以及我做错了什么吗? Any pointers are appreciated. 任何指针表示赞赏。

Thanks! 谢谢!

In this example, I created a .dropdown-submenu class for multi-level dropdowns 在此示例中,我为多层下拉列表创建了一个.dropdown-submenu类。

Note that I added jQuery to open the multi-level dropdown on click. 请注意,我添加了jQuery以在单击时打开多级下拉列表。

 .dropdown-submenu { position: relative; } .dropdown-submenu .dropdown-menu { top: 0; left: 100%; margin-top: -1px; } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <div class="container"> <div class="dropdown"> <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">Tutorials <span class="caret"></span></button> <ul class="dropdown-menu"> <li><a tabindex="-1" href="#">HTML</a></li> <li><a tabindex="-1" href="#">CSS</a></li> <li class="dropdown-submenu"> <a class="test" tabindex="-1" href="#">New dropdown <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a tabindex="-1" href="#">2nd level dropdown</a></li> <li><a tabindex="-1" href="#">2nd level dropdown</a></li> <li class="dropdown-submenu"> <a class="test" href="#">Another dropdown <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="#">3rd level dropdown</a></li> <li><a href="#">3rd level dropdown</a></li> </ul> </li> </ul> </li> </ul> </div> </div> <script> $(document).ready(function(){ $('.dropdown-submenu a.test').on("click", function(e){ $(this).next('ul').toggle(); e.stopPropagation(); e.preventDefault(); }); }); </script> 

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

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