最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - click outside bootstrap menu to close it - Stack Overflow

programmeradmin4浏览0评论

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:

// 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

and yes i tried this already, not working:

similar question

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:

// 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

and yes i tried this already, not working:

similar question

Share Improve this question edited May 23, 2017 at 12:09 CommunityBot 11 silver badge asked Jan 19, 2016 at 13:22 JohanJohan 71 gold badge1 silver badge6 bronze badges 2
  • where to check on website? – Hardik Gondalia Commented Jan 19, 2016 at 13:35
  • its there iwebdesign.se – Johan Commented Jan 19, 2016 at 14:55
Add a ment  | 

3 Answers 3

Reset to default 2

Assuming you want to do something different when clicking outside of the menu (i.e. 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/L3qg4pa8/5/ shows the concept, roughly.

Of course, you will need to replace '.navigation' in the .closest() statement with the appropriate selector for the container of your navigation.

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

Just copy the code and past your custome script or index.html

thank's Remy click outside to hide bootstrap toggle menu close(hide) it

Here the answer :

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

Hope it's help :)

Remy

发布评论

评论列表(0)

  1. 暂无评论