简体   繁体   English

多级可折叠Bootstrap侧导航菜单有问题

[英]Trouble with multi-level collapsible Bootstrap side-nav menu

I've been combing around looking for an example of the same problem I'm having but no luck yet. 我一直在寻找一个同样问题的例子,但我还没有运气。 I'm trying to create a multi-level collapsible side navigation in bootstrap using bootstrap.js. 我正在尝试使用bootstrap.js在bootstrap中创建一个多级可折叠侧导航。

My problem is that I've added in the second level but when I click the list item to trigger it to open it collapses the whole menu. 我的问题是我已经添加了第二级但是当我单击列表项以触发它打开时它会折叠整个菜单。 When I re-open the menu, the second-level menu is open also. 当我重新打开菜单时,第二级菜单也会打开。 My code is below: 我的代码如下:

          <div class="sidebar-nav">
      <p class="sidenav-header">Units and Lessons:</p>
      <ul class="nav nav-list">
        <li class="nav-header" data-toggle="collapse" data-target="#content-areas"> <span class="nav-header-primary">
                Content Areas
        </span>
            <ul class="nav nav-list collapse" id="content-areas">
                <li><a href="#" title="Title">All</a></li>
                <li><a href="#" title="Title">Urban Planning</a></li>
                <li><a href="#" title="Title">Sustainability</a></li>
                <li><a href="#" title="Title">Public Administration</a></li>
                <li data-toggle="collapse" data-target="#nav-health" data-parent="#content-areas"><a href="#" title="Title">Health</a>
                  <ul class="nav-secondary nav-list collapse" id="nav-health">
                    <li><a href="#" title="Title">Introduction</a></li>
                    <li><a href="#" title="Title">Lesson: What is Epilepsy?</a></li>
                    <li><a href="#" title="Title">Lesson: Pathology</a></li>
                  </ul>
                </li>
            </ul>
        </li>
          <li class="nav-header" data-toggle="collapse" data-target="#languages"> <span class="nav-header-primary">
                Languages
        </span>
            <ul class="nav nav-list collapse" id="languages">
                <li><a href="#" title="Title">All</a></li>
                <li><a href="#" title="Title">Arabic</a></li>
                <li><a href="#" title="Title">Turkish</a></li>
                <li><a href="#" title="Title">Hebrew</a></li>
            </ul>
        </li>
    </ul>  

      </div>

The specific section I'm talking about is under the "Health" list item under "Content Areas". 我正在谈论的具体部分是在“内容区域”下的“健康”列表项下。

Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thanks! 谢谢!

The problem with your markup is that whole menu is collapsed when you try to click within the list. 标记的问题是当您尝试在列表中单击时整个菜单会折叠。 For example, if you click All inside of Content Areas it collapse the Content Areas. 例如,如果单击“内容区域内的全部”,则会折叠“内容区域”。 The same is happening for the second level menu, Health in your case. 对于您的情况,第二级菜单Health也是如此。

This is caused because you don't have an accordion-heading specified. 这是因为您没有指定手风琴标题。 Take a look into Bootstrap Collapse Documentation where you will find an example like this: 查看Bootstrap Collapse Documentation ,您将在其中找到如下示例:

<div class="accordion-heading">
  <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne">
    Collapsible Group Item #1
  </a>

Once you specify the heading your second level nav should work fine. 指定标题后,您的第二级导航应该可以正常工作。

I have created a fiddle for you to verify. 我创造了一个小提琴供你验证。 I modified the Content Areas according to the Documentation and the second level menu worked fine. 我根据文档修改了内容区域,第二级菜单工作正常。 However, I did not modify the Languages menu items so that you can see how it's acting (try clicking within the list once the Languages is expanded) 但是,我没有修改语言菜单项,以便您可以看到它的行为(在语言扩展后尝试在列表中单击)

While Similar to Pitamber's example I would not add some of the extra classes, especially the extra div with class "accoridan-heading", just apply that class to the a tag as so. 虽然与Pitamber的例子类似,但我不会添加一些额外的类,特别是带有“accoridan-heading”类的额外div ,只需将该类应用于a标签即可。

<ul class="nav nav-list">
  <li>
    <a>Menu Item with No Sub-Items</a>
  </li>
  <li>
    <a class="accordion-heading" data-toggle="collapse" data-target="#submenu">
      <span class="nav-header-primary">Menu Item with Sub-Items <b class="caret"></b></span>
    </a>
    <ul class="nav nav-list collapse" id="submenu">
      <li><a href="#" title="Title">Sub Item 1</a></li>
      <li><a href="#" title="Title">Sub Item 2</a></li>
      <li><a href="#" title="Title">Sub Item 3</a></li>
      <li><a href="#" title="Title">Sub Item 4</a></li>
    </ul>
  </li>
</ul>

You can go further and add CSS and or some JS to place some contrast on the sub-menu and also to flip/change the caret icon when a menu item is expanded and collapsed. 您可以进一步添加CSS和/或一些JS以在子菜单上放置一些对比度,并在菜单项展开和折叠时翻转/更改插入符号图标。

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

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