Clikc外部切换菜单 - 隐藏菜单和关闭按钮

时间:2017-01-26 12:00:31

标签: jquery drop-down-menu zurb-foundation toggle

我使用jqueru和基础zurb数据响应切换。如何在外部点击时关闭菜单。我尝试了这一个代码,但是当我点击外面时隐藏了,并且还点击链接它也是隐藏的。如何防止这一个。

  $(window).click(function (e) {
        var isActive =  $('#mobile-nav-menu').hasClass('active');
       if(isActive){
           $('#mobile-nav-menu').removeClass("active").css('display', 'none');
           e.preventDefault();
            e.stopPropagation();
       }
    });

<div class="mobile-menu no-js" data-responsive-toggle="mobile-nav-menu" data-hide-for="large" data-close-on-click>
  <div class="row column">
    <div class="top-bar-left">
      <a class="logo" href="/html/index.html"><img src="../images/egl-logo.png" alt="logo"></a>
    </div>
    <div class="top-bar-right"><span class="icon icon-search color-white"></span>
      <div class="menu-block show-for-medium">
        <ul class="menu">
          <li><a class="primary small" href="analizes-grozs.html"><span class="icon icon-cart"><span class="path1"></span><span class="path2"></span><span class="path3"></span><span class="path4"></span></span>Grozs</a></li>
        </ul>
      </div>
      <button class="menu-toggle" type="button" data-toggle></button>
    </div>
  </div>
</div>
<div class="navigation-mobile no-js hide-for-large" id="mobile-nav-menu">
  <div class="row column padd-off">
    <div class="navigation-mobile-search">
      <input type="text" placeholder="Meklēt"><span class="icon icon-search"></span>
    </div>
    <div class="navigation-mobile-wrapper">

      <div class="menu-block">
        <ul class="vertical menu" data-accordion-menu>
          <li></li>
        </ul>
      </div>
    </div>
  </div>
</div>

我也试过这个

:change

1 个答案:

答案 0 :(得分:0)

这对我有用How do I detect a click outside an element?

  $(window).click(function () {
        var isActive =  $('#mobile-nav-menu').hasClass('active');
       if(isActive){
           $('#mobile-nav-menu').removeClass("active").css('display', 'none');
           $('.menu-toggle').toggleClass('open');

       }
    });
    $('.navigation-mobile-wrapper').click(function(event){
    event.stopPropagation();
    });