如何通过外部点击关闭导航?

时间:2017-03-13 20:29:09

标签: javascript jquery

我想在窗口的其余部分检测菜单类.tab-nav-menu之外的点击,并添加一个事件来关闭菜单,其中包含类似的关闭动画。



sudo ./solarisstudio.sh --non-interactive --libraries-only --javahome <our java location>
&#13;
&#13;
&#13;

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

简化的“外部点击”jQuery脚本:

$(document).ready(function () {

  $(document).on('click', function (e) {
    var clickedEl = $(e.target);
    var outsideClicker = $('#clicker');

    if ( !(clickedEl.is(outsideClicker)
 || outsideClicker.has(clickedEl).length > 0) ) { // I flipped this so you can just omit the else
      console.log('I clicked outside the target!'); // do whatever you need to do here-- maybe call a function that closes the menu...
    } else {
      console.log('all good'); // if you don't have an else just get rid of this
    }
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>

  <h1> A title </h1>
  <p> A paragraph and a <b id="clicker">thing <span>to</span> click </b></p>

</div>

您可以根据自己的目的推断这一点。