【问题标题】:How to close the navigation bar by clicking outside?如何通过点击外部关闭导航栏?
【发布时间】:2019-02-10 23:41:13
【问题描述】:

我有一个带有 Bootstrap 3.3.7 主题的 Drupal 8.6.8 站点

我希望在单击外部时关闭导航菜单。我尝试了代码:

(function ($) {
  'use strict';

  $(document).click(function (event) {
    if (!$(event.target).closest('#navbar-collapse-first').length) {
      $('.navbar-collapse-first').collapse('hide');
    }
  });

  $(document).click(function (event) {
    if (!$(event.target).closest('#navbar-collapse-second').length) {
      $('.navbar-collapse-second').collapse('hide');
    }
  });

}(jQuery));

https://css-tricks.com/dangers-stopping-event-propagation/

它不起作用,如果我在导航菜单之外单击,它不起作用此代码仅在我删除第二个或离开第二个并删除第一个时才有效。

如何在 2 菜单上应用这个?

更新:

我找到了答案:

(function ($) {
  'use strict';

  $(document).click(function (event) {
    if (!$(event.target).closest('#navbar-collapse-first').length) {
      $('.navbar-collapse-first').collapse('hide');
    }
    if (!$(event.target).closest('#navbar-collapse-second').length) {
      $('.navbar-collapse-second').collapse('hide');
    }
  });

}(jQuery));

【问题讨论】:

    标签: javascript jquery twitter-bootstrap navigation collapse


    【解决方案1】:

    你可以使用这个功能

    function OnwindowClick(elem , action){
        $(document).on('click',function(e){
            if (!$(elem).is(e.target) // if the target of the click isn't the container...
                && $(elem).has(e.target).length === 0) // ... nor a descendant of the container
            {
                action();
            }
        });
    }
    

    并像使用它一样使用它

    // OnwindowClick(elem , action) add the prevent elements in `elem` something like this
    OnwindowClick('#navbar-collapse-first , #navbar-collapse-second', function(){
       $('.navbar-collapse-first, .navbar-collapse-second').collapse('hide');
    });
    

    注意事项:

    • 不用.closest()直接使用选择器

    • elem是防止文档点击它的元素

    附加:您仍然需要将按钮添加到elem ..#navbar-collapse-first , #navbar-collapse-second , button1_Selector , button2_Selector

    示例如何使用此功能

    $(document).ready(function(){
      $('button.clickable').on('click' , function(){
        $(this).text($(this).text() == 'Like' ? 'Dislike' : 'Like');
      });
      
      OnwindowClick('button.clickable' , function(){
        $('button.clickable').fadeOut(400);
        setTimeout(function(){
          $('button.clickable').fadeIn(400);
        } , 5000);
      });
    });
    
    
    function OnwindowClick(elem , action){
        $(document).on('click',function(e){
            if (!$(elem).is(e.target) // if the target of the click isn't the container...
                && $(elem).has(e.target).length === 0) // ... nor a descendant of the container
            {
                action();
            }
        });
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <button class="clickable">Like</button>

    【讨论】:

    • 您好,将逻辑放在它自己的函数中是一个很好的方法。 function OnwindowClick(elem, action) 也可以进入$(document).ready(function(){})。他们把它放在外面有特定的理由吗?
    • @LebCit 你可以在它工作的时候在任何你想要的地方使用它.. 但是由于它在 ready 函数中,直到整个 DOM 被解析之后才会被定义.. 你可以采取看functions inside or outside jquery document ready
    【解决方案2】:

    你试过这个吗:

    
        $(document).click(function (event) {
        if (!$(event.target).closest('#navbar-collapse-second, #navbar-collapse-second').length) {
          $('.navbar-collapse-second, .navbar-collapse-second').collapse('hide');
        }
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-26
      • 2018-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      相关资源
      最近更新 更多