Bootstrap垂直侧边栏 - 带子菜单的折叠式菜单

时间:2017-06-22 23:39:01

标签: javascript html twitter-bootstrap

this Bootply demo中,当我们点击第3项时,它会扩展其子菜单 - 然后当我们点击第4项时,项目3会折叠。扩展项目4并单击项目3以展开时也是如此。我需要这种行为。但是问题:如果点击第1项或第2项,我们怎样才能使第3项(或4项)崩溃(如果它已展开)? 注意:我在最新版本的Chrome上注意到,如果项目3(或4)(在上面的演示中)展开,您点击第1项或第2项第3项(或4项) )继续扩大。

所以,我们将来不会松开上面的Bootply演示,这里是他们的代码供参考:

<div class="container-fluid">
  <div class="row">
    <div class="col-md-2">      
      <!-- menu -->
      <div id="MainMenu">
        <div class="list-group panel">
          <a href="#" class="list-group-item list-group-item-success" data-parent="#MainMenu">Item 1</a>
          <a href="#" class="list-group-item list-group-item-success" data-parent="#MainMenu">Item 2</a>
          <a href="#demo3" class="list-group-item list-group-item-success" data-toggle="collapse" data-parent="#MainMenu">Item 3 <i class="fa fa-caret-down"></i></a>
          <div class="collapse" id="demo3">
            <a href="#SubMenu1" class="list-group-item" data-toggle="collapse" data-parent="#SubMenu1">Subitem 1 <i class="fa fa-caret-down"></i></a>
            <div class="collapse list-group-submenu" id="SubMenu1">
              <a href="#" class="list-group-item" data-parent="#SubMenu1">Subitem 1 a</a>
              <a href="#" class="list-group-item" data-parent="#SubMenu1">Subitem 2 b</a>
              <a href="#SubSubMenu1" class="list-group-item" data-toggle="collapse" data-parent="#SubSubMenu1">Subitem 3 c <i class="fa fa-caret-down"></i></a>
              <div class="collapse list-group-submenu list-group-submenu-1" id="SubSubMenu1">
                <a href="#" class="list-group-item" data-parent="#SubSubMenu1">Sub sub item 1</a>
                <a href="#" class="list-group-item" data-parent="#SubSubMenu1">Sub sub item 2</a>
              </div>
              <a href="#" class="list-group-item" data-parent="#SubMenu1">Subitem 4 d</a>
            </div>
            <a href="javascript:;" class="list-group-item">Subitem 2</a>
            <a href="javascript:;" class="list-group-item">Subitem 3</a>
          </div>
          <a href="#demo4" class="list-group-item list-group-item-success" data-toggle="collapse" data-parent="#MainMenu">Item 4  <i class="fa fa-caret-down"></i></a>
          <div class="collapse" id="demo4">
            <a href="#" class="list-group-item">Subitem 1</a>
            <a href="#" class="list-group-item">Subitem 2</a>
            <a href="#" class="list-group-item">Subitem 3</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

<!doctype html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<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>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<script>
$(document).ready(function(){
    $(".onetwo").click(function(){
        $("#demo3").hide();
        $("#demo4").hide();
    });
});

$(document).ready(function(){
    $(".three").click(function(){
        $("#demo3").toggle();
        $("#demo4").hide();
    });
}); 

$(document).ready(function(){
    $(".four").click(function(){
         $("#demo4").toggle();
         $("#demo3").hide();
    });
});
</script>

<style>

</style>

</head>
<body>

<div class="container-fluid">
<div class="row">
<div class="col-md-2">      
  <!-- menu -->
  <div id="MainMenu">
    <div class="list-group panel">
      <a href="#" class="list-group-item list-group-item-success onetwo" data-parent="#MainMenu">Item 1</a>

      <a href="#" class="list-group-item list-group-item-success onetwo" data-parent="#MainMenu">Item 2</a>

      <a href="#demo3" class="list-group-item list-group-item-success three" data-toggle="collapse" data-parent="#MainMenu ">Item 3 <i class="fa fa-caret-down"></i></a>
      <div class="collapse" id="demo3">
        <a href="#SubMenu1" class="list-group-item" data-toggle="collapse" data-parent="#SubMenu1">Subitem 1 <i class="fa fa-caret-down"></i></a>
        <div class="collapse list-group-submenu" id="SubMenu1">
          <a href="#" class="list-group-item" data-parent="#SubMenu1">Subitem 1 a</a>
          <a href="#" class="list-group-item" data-parent="#SubMenu1">Subitem 2 b</a>    
          <a href="#SubSubMenu1" class="list-group-item" data-toggle="collapse" data-parent="#SubSubMenu1">Subitem 3 c <i class="fa fa-caret-down"></i></a>
          <div class="collapse list-group-submenu list-group-submenu-1" id="SubSubMenu1">
            <a href="#" class="list-group-item" data-parent="#SubSubMenu1">Sub sub item 1</a>
            <a href="#" class="list-group-item" data-parent="#SubSubMenu1">Sub sub item 2</a>
          </div>
          <a href="#" class="list-group-item" data-parent="#SubMenu1">Subitem 4 d</a>
        </div>
        <a href="javascript:;" class="list-group-item">Subitem 2</a>
        <a href="javascript:;" class="list-group-item">Subitem 3</a>
      </div>

      <a href="#demo4" class="list-group-item list-group-item-success four" data-toggle="collapse" data-parent="#MainMenu">Item 4  <i class="fa fa-caret-down"></i></a>
      <div class="collapse" id="demo4">
        <a href="#" class="list-group-item">Subitem 1</a>
        <a href="#" class="list-group-item">Subitem 2</a>
        <a href="#" class="list-group-item">Subitem 3</a>
      </div>
    </div>
  </div>
</div>
</div>
</div>


</body>
</html>