简体   繁体   English

单击外部引导程序菜单以将其关闭

[英]click outside bootstrap menu to close it

I tried to find solution to close bootstrap menu when clicking outside of it(in mobile window size), but cant get it to work, I get it to work when clicking one of the 'a' links by this code: 我尝试找到在外部(在移动窗口大小下)单击以关闭引导菜单的解决方案,但无法使其正常工作,而在通过以下代码单击“ a”链接之一时,它就可以正常工作:

// menu buttons collapses when clicking a link
    $('document').ready(function() 
{
    if ($('a').on('click', function() 
            { 
                $('.collapse, #mainContainer').removeClass('in'); 
                $('.navbar-toggle').toggleClass('collapsed'); // button
            }));
});

but how to close menu by clicking outside the menu navbar? 但是如何通过单击菜单导航栏外部来关闭菜单?

here's my page that shows the problem iwebdesign.se 这是我的页面,显示问题iwebdesign.se

and yes i tried this already, not working: 是的,我已经尝试过了,不能正常工作:

similar question 类似的问题

  $(document).on('click',function(){
   $('.collapse').collapse('hide');
});

Just copy the code and past your custome script or index.html 只需复制代码并经过您的自定义脚本或index.html

thank's Remy click outside to hide bootstrap toggle menu close(hide) it 感谢雷米(Remy)在外面单击以隐藏引导程序切换菜单,关闭(隐藏)它

Here the answer : 这里的答案:

   (document).on('click',function(){
   $('.collapse').collapse('hide');
})

Hope it's help :) 希望对您有所帮助:)

Remy 雷米

Assuming you want to do something different when clicking outside of the menu (ie collapse the menu) than what happens when you click inside the menu, you probably want something like this to determine if you're clicking inside or outside of the menu: 假设您想在菜单外部单击时(即折叠菜单)与在菜单内部单击时想做的事情有所不同,那么您可能希望这样来确定您是在菜单内部还是在菜单外部单击:

$('body').click(function(event){
  // check if the clicked element is a descendent of navigation 
  if ($(event.target).closest('.navigation').length) {
    return; //do nothing if event target is within the navigation
  } else {
  // do something if the event target is outside the navigation
     // code for collapsing menu here...
  }
});

https://jsfiddle.net/L3qg4pa8/5/ shows the concept, roughly. https://jsfiddle.net/L3qg4pa8/5/大致显示了这个概念。

Of course, you will need to replace '.navigation' in the .closest() statement with the appropriate selector for the container of your navigation. 当然,您需要将.closest()语句中的'.navigation'替换为导航容器的相应选择器。

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

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