使用bootstrap的可折叠菜单

时间:2014-09-01 11:37:17

标签: javascript jquery twitter-bootstrap jsp

我创建了一个简单的垂直导航栏,它有一些菜单项,而且每个菜单项都有一些子菜单项。当我点击菜单时,它会展开以打开其子菜单项,当我点击另一个菜单时,它也会展开。

问题是当我点击其他菜单项时,我希望一个菜单项自动崩溃,即我希望一次只展开一个菜单项。

<nav class="navbar navbar-default"> 
<div class="container-fluid">   
<ul class="nav nav-list bs-sidenav ">
      <li class="nav-header">
      <a href="#" data-toggle="collapse" data-target="#One"> <font color="grey">Main One</font></a>
        <ul class = "collapse" id = "One">
            <li ><a href="http://Input_Form.jsp"> <font color="grey">Sub One</font></a></li>
            <li><a href="http://2_Input_Form.jsp"><font color="grey">Sub Two</font></a></li>
            <li><a href="http://3_Input_Form.jsp"><font color="grey">Sub Three</font></a></li>
        </ul>
    </li>   
      <li class="nav-header">
      <a href="#" data-toggle="collapse" data-target="#Two"> <font color="grey">Main Two</font></a>
        <ul class = "collapse" id = "Two">
            <li ><a href="http://Input_Form.jsp"> <font color="grey">Sub One</font></a></li>
            <li><a href="http://2_Input_Form.jsp"><font color="grey">Sub Two</font></a></li>
            <li><a href="http://3_Input_Form.jsp"><font color="grey">Sub Three</font></a></li>
        </ul>
    </li>  
</ul>
</div>
</nav>

4 个答案:

答案 0 :(得分:1)

使用collapse plugin

<div class="panel-group" id="accordion">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
          Collapsible Group Item #2
        </a>
      </h4>
    </div>
    <div id="collapseTwo" class="panel-collapse collapse">
      <div class="panel-body">
      </div>
    </div>
  </div>
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree">
          Collapsible Group Item #3
        </a>
      </h4>
    </div>
    <div id="collapseThree" class="panel-collapse collapse">
      <div class="panel-body">
      </div>
    </div>
  </div>
</div>

答案 1 :(得分:0)

试试这个:

在bootstrap中使用accordion

DEMO

 <div class="panel-group" id="accordion">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
          Collapsible 1
        </a>
      </h4>
    </div>
    <div id="collapseTwo" class="panel-collapse collapse">
      <div class="panel-body">
          <ul>
            <li ><a href="http://Input_Form.jsp"> <font color="grey">Sub One</font></a></li>
            <li><a href="http://2_Input_Form.jsp"><font color="grey">Sub Two</font></a></li>
            <li><a href="http://3_Input_Form.jsp"><font color="grey">Sub Three</font></a></li>
        </ul>
      </div>
    </div>
  </div>
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree">
          Collapsible 2
        </a>
      </h4>
    </div>
    <div id="collapseThree" class="panel-collapse collapse">
      <div class="panel-body">
           <ul>
            <li ><a href="http://Input_Form.jsp"> <font color="grey">Sub One</font></a></li>
            <li><a href="http://2_Input_Form.jsp"><font color="grey">Sub Two</font></a></li>
            <li><a href="http://3_Input_Form.jsp"><font color="grey">Sub Three</font></a></li>
        </ul>
      </div>
    </div>
  </div>
</div>

答案 2 :(得分:0)

这是demo

这是代码;

<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
    <div class="panel panel-default">
        <div class="panel-heading" role="tab" id="headingOne">
            <h4 class="panel-title"> <a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne"> Main One </a> </h4>
        </div>
        <div aria-expanded="false" id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
            <div class="panel-body">
                <ul>
                    <li ><a href="http://Input_Form.jsp"> <font color="grey">Sub One</font></a></li>
                    <li><a href="http://2_Input_Form.jsp"><font color="grey">Sub Two</font></a></li>
                    <li><a href="http://3_Input_Form.jsp"><font color="grey">Sub Three</font></a></li>
                </ul>
            </div>
        </div>
    </div>
    <div class="panel panel-default">
        <div class="panel-heading" role="tab" id="headingTwo">
            <h4 class="panel-title"> <a class="collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"> Main Two </a> </h4>
        </div>
        <div aria-expanded="false" id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
            <div class="panel-body">
                <ul>
                    <li ><a href="http://Input_Form.jsp"> <font color="grey">Sub One</font></a></li>
                    <li><a href="http://2_Input_Form.jsp"><font color="grey">Sub Two</font></a></li>
                    <li><a href="http://3_Input_Form.jsp"><font color="grey">Sub Three</font></a></li>
                </ul>
            </div>
        </div>
    </div>    
</div>

答案 3 :(得分:0)

这个问题非常简单,简单的css可以帮助你获得理想的结果。

https://jsfiddle.net/arunzo/dh2zhh2k/

nav ul ul {
    display:none;
}

a:focus + ul
{
    display:block;
}