javascript - Dropdown inside dropdown -
i have navbar main dropdown li tag called a, when display dropdown shows a-1 , a-2 options, a-2 it's dropdown other options, tried copying same code if main dropdown doesn't works properly, have code right here
however below code.
$(document).ready(function () { console.log("hi..."); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> <nav class="navbar navbar-default navbar-static-top" id="nav"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> <span class="sr-only">toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">apbpv</a> </div> <div id="navbar" class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">a <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="#">a-1</a></li> <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">a-2<b class="caret"></b></a> <ul class="dropdown-menu"> <li><a href="#">a-2-1</a></li> <li><a href="#">a-2-2</a></li> </ul> </li> </ul> </li> </ul> </div> </div> </nav>
you need javascript work:
$(document).ready(function(){ $('a.dropdown-toggle').on("click", function(e){ $(this).closest('li').toggleclass('open'); e.stoppropagation(); e.preventdefault(); }); });
here explanation regarding above code:
once click on anchor has dropdown-toggle class (which mean - it's anchor should open next drop-down menu) - set classopen
on aboveli
element (which tells next-menu open) , prevent default behavior of current element.
$(document).ready(function(){ $('a.dropdown-toggle').on("click", function(e){ $(this).closest('li').toggleclass('open'); e.stoppropagation(); e.preventdefault(); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> <nav class="navbar navbar-default navbar-static-top" id="nav"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar"> <span class="sr-only">toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">apbpv</a> </div> <div id="navbar" class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">a <span class="caret"></span></a> <ul class="dropdown-menu"> <li><a href="#">a-1</a></li> <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">a-2<b class="caret"></b></a> <ul class="dropdown-menu"> <li><a href="#">a-2-1</a></li> <li><a href="#">a-2-2</a></li> </ul> </li> </ul> </li> </ul> </div> </div> </nav>
Comments
Post a Comment