Twitter Bootstrap responsive menu/small devices: close/collapse menu on clicking a menu item

一世执手 提交于 2019-12-05 10:53:25

Thanks for the fix Skelly. It worked great on the "mobile" menu, but unfortunately it was causing a visual defect in "desktop" mode as it was targeting .nav-collapse, and this class is active in both modes. So, I made a slight modification:

$('.nav li a').on('click',function(){
    $('.navbar-collapse.in').collapse('hide');
})

This modification will only invoke the 'hide' functionality when the menu is in "mobile" mode.

Note: This is only tested on Bootstrap 3.

You can try something like..

$('.nav li a').on('click',function(){
    $('.nav-collapse').collapse('hide');
})

This will work for menu links that are referencing '#'. The menu should already be collapsing if your menu links navigate to another page on the site as this would cause a full page refresh.

You can do that by using HTML, add data-toggle="collapse" data-target=".in" to anchor

Example:

<li><a href="#" data-toggle="collapse" data-target=".in">Link</a></li>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!