简体   繁体   中英

Bootstrap ancor doen't collapse mobile navbar

I am using Bootstrap v3.2.0 and have created this navbar:

<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <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="#home">Site Title</a>
        </div>
        <div class="collapse navbar-collapse">
            <ul class="nav navbar-nav">
                <li><a href="#home">Home</a></li>
                <li><a href="#programming">Programming</a></li>
                <li><a href="#robotics">Robotics</a></li>
                <li><a href="#ece">Electrical Engineering</a></li>
                <li><a href="#web">Web Development</a></li>
                <li><a href="#contact">Contact</a></li>
            </ul>
        </div>
    </div>
</nav>

As you can see, the site uses anchors to navigate the page (it is one long page). It works on mobile, but the menu does not collapse when I click on a link, and I have to click on the button again to see the page. Does anyone have a suggestion on how to collapse it when a link is clicked? I was thinking if I added data-toggle="collapse" data-target=".navbar-collapse" to each link, it would work, but it didn't.

Add this to your js. please see here for demo: http://jsbin.com/lapon/1/edit

$('.nav a').on('click', function(){
   $(".navbar-toggle").click()
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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