简体   繁体   English

当您单击外部时,Bootstrap 3 关闭导航菜单

[英]Bootstrap 3 close nav menu when you click outside

I am using the following jQuery:我正在使用以下 jQuery:

            jQuery(document).click(function (event) {
               var clickover = jQuery(event.target);
               var _opened = jQuery(".navbar-collapse2").hasClass("in");
               if (_opened === true && !clickover.hasClass("navbar-toggle")) {
                  jQuery("button.navbar-toggle").click();
               }
            });

The problem is within navbar-collapse I have some sub menus and when I click to expand/collapse them.问题出在 navbar-collapse 我有一些子菜单,当我单击展开/折叠它们时。 It also collapses the main nav menu.它还会折叠主导航菜单。

You should probably check if your clicked element is not a child of .navbar-toggle .您可能应该检查您单击的元素是否不是.navbar-toggle的子元素。 You can do that with the closest() function.你可以用closest() function 来做到这一点。 This will find the first parent that matches the selector.这将找到与选择器匹配的第一个父级。

if (_opened === true && !clickover.hasClass("navbar-toggle") && clickover.closest(".navbar-toggle").length === 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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