Paulo Roberto Paulo Roberto - 4 months ago 35
Javascript Question

Twitter Bootstrap nav collapse Hide on click

When I click on a menu item , the menu does not close , already checked the imports of JS and CSS and seemingly everything OK , there is something else I need to do to run the menu?

<!-- Fixed navbar -->
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>

</div>
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav">
<li class="active"><a href="#home" class="active scroll">Home</a></li>
<li><a href="#about" class="scroll">Sobre</a></li>
<li><a href="#services" class="scroll">Serviços</a></li>
<li><a href="#testimonials" class="scroll">Opiniões</a></li>
<li><a href="#gallery" class="scroll">Gallery</a></li>
<li><a href="#news" class="scroll">News</a></li>
<li><a href="#contact" class="scroll">Contact</a></li>
</ul>
</div>
</div>
</nav>


image

Answer

for me works...

$(document).ready(function(){
  $('.navbar-collapse a').on('click', function(){
            $('.navbar-toggle').click();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<nav class="navbar  navbar-default  navbar-fixed-top">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
  </button>

        </div>
        <div class="collapse navbar-collapse" id="myNavbar">
            <ul class="nav navbar-nav">
                <li class="active"><a href="#home" class="active scroll">Home</a></li>
                <li><a href="#about" class="scroll">Sobre</a></li>
                <li><a href="#services" class="scroll">Serviços</a></li>
                <li><a href="#testimonials" class="scroll">Opiniões</a></li>
                <li><a href="#gallery" class="scroll">Gallery</a></li>
                <li><a href="#news" class="scroll">News</a></li>
                <li><a href="#contact" class="scroll">Contact</a></li>
            </ul>
        </div>
    </div>
</nav>