Home >Web Front-end >CSS Tutorial >How to Close a Collapsible Bootstrap Navbar on Link Click?
Closing a Collapsible Bootstrap Navbar on Click
Bootstrap 5 (beta)
Bootstrap 4
Bootstrap 3
Here's the detailed solution for Bootstrap 3:
markup:
<ul class="navbar-nav mr-auto"> <li class="nav-item active" data-toggle="collapse" data-target=".navbar-collapse.show"> <a class="nav-link" href="#home">Home <span class="sr-only">(current)</span></a> </li> <li class="nav-item" data-toggle="collapse" data-target=".navbar-collapse.show"> <a class="nav-link" href="#about-us">About</a> </li> <li class="nav-item" data-toggle="collapse" data-target=".navbar-collapse.show"> <a class="nav-link" href="#pricing">Pricing</a> </li> </ul>
jQuery:
$('.navbar-nav>li>a').on('click', function(){ $('.navbar-collapse').collapse('hide'); });
This method will hide the collapsible navbar after a link is clicked, enhancing user experience and navigation flow.
The above is the detailed content of How to Close a Collapsible Bootstrap Navbar on Link Click?. For more information, please follow other related articles on the PHP Chinese website!