避免在内部单击时关闭下拉菜单

I have a Twitter Bootstrap dropdown menu. As all Twitter Bootstrap users know, the dropdown menu closes on click (even clicking inside it).

To avoid this, I can easily attach a click event handler on the dropdown menu and simply add the famous event.stopPropagation().

<ul class="nav navbar-nav">
  <li class="dropdown mega-dropdown">
    <a href="javascript:;" class="dropdown-toggle" data-toggle="dropdown">
      <i class="fa fa-list-alt"></i> Menu item 1
      <span class="fa fa-chevron-down pull-right"></span>
    </a>
    <ul class="dropdown-menu mega-dropdown-menu">
      <li>
        <div id="carousel" class="carousel slide" data-ride="carousel">
          <ol class="carousel-indicators">
            <li data-slide-to="0" data-target="#carousel"></li>
            <li class="active" data-slide-to="1" data-target="#carousel"></li>
          </ol>
          <div class="carousel-inner">
            <div class="item">
              <img alt="" class="img-rounded" src="img1.jpg">
            </div>
            <div class="item active">
              <img alt="" class="img-rounded" src="img2.jpg">
            </div>
          </div>
          <a data-slide="prev" role="button" href="#carousel" 
             class="left carousel-control">
            <span class="glyphicon glyphicon-chevron-left"></span>
          </a>
          <a data-slide="next" role="button" href="#carousel" 
             class="right carousel-control">
            <span class="glyphicon glyphicon-chevron-right"></span>
          </a>
        </div>
      </li>
    </ul>
  </li>
</ul>

This looks easy and a very common behavior, however, and since carousel-controls (as well as carousel indicators) event handlers are delegated to the document object, the click event on these elements (prev/next controls, ...) will be “ignored”.

$('ul.dropdown-menu.mega-dropdown-menu').on('click', function(event){
    // The event won't be propagated up to the document NODE and 
    // therefore delegated events won't be fired
    event.stopPropagation();
});

Relying on Twitter Bootstrap dropdown hide/hidden events is not a solution for the following reasons:

  • The provided event object for both event handlers does not give reference to the clicked element
  • I don't have control over the dropdown menu content so adding a flag class or attribute is not possible

This fiddle is the normal behavior and this fiddle is with event.stopPropagation() added.

Update

Thanks to Roman for his answer. I also found an answer that you can find below.

Gil2020/03/24 15:11:59

我知道已经有一个建议使用表格的先前答案,但是提供的标记不正确/不理想。这是最简单的解决方案,根本不需要JavaScript,也不会破坏您的下拉菜单。适用于Bootstrap 4。

<form class="dropdown-item"> <!-- Your elements go here --> </form>

飞云2020/03/24 15:11:59

而不是编写一些javascript或jquery代码(重新发明轮子)。以上情况可以通过引导程序自动关闭选项进行管理。您可以提供两个值之一来自动关闭

  1. 始终-(默认)单击其任何元素时自动关闭下拉菜单。

  2. outsideClick-仅当用户单击下拉列表外的任何元素时,才自动关闭下拉列表。

  3. 禁用-禁用自动关闭

看一下下面的plunkr:

http://plnkr.co/edit/gnU8M2fqlE0GscUQtCWa?p=preview

uib-dropdown auto-close="disabled" 

希望这可以帮助 :)

神无2020/03/24 15:11:58

借助Angular2 Bootstrap,您可以在大多数情况下使用nonInput:

<div dropdown autoClose="nonInput">

nonInput-(默认值)在单击其任何元素时自动关闭下拉菜单-只要被单击的元素不是输入或文本区域。

https://valor-software.com/ng2-bootstrap/#/dropdowns

小小2020/03/24 15:11:57

我尝试了这个简单的事情,它就像一个魅力。

我将下拉菜单元素从更改<div><form>,效果很好。

<div class="nav-item dropdown" >
  <a href="javascript:;" class="nav-link dropdown-toggle" data-toggle="dropdown">
   Click to open dropdown
 </a>
 <form class="dropdown-menu   ">
  <ul class="list-group text-black">
     <li class="list-group-item"  >
     </li>
     <li class="list-group-item"   >
     </li>
  </ul>
</form>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>


<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>


<div class="nav-item dropdown" >
  <a href="javascript:;" class="nav-link dropdown-toggle" data-toggle="dropdown">
   Click to open dropdown
 </a>
 <form class="dropdown-menu   ">
  <ul class="list-group text-black">
     <li class="list-group-item"  >
      List Item 1
     </li>
     <li class="list-group-item"   >
         LI 2<input class="form-control" />
     </li>
     <li class="list-group-item"   >
        List Item 3
     </li>
  </ul>
</form>

十三2020/03/24 15:11:57

这可能会有所帮助:

$("dropdownmenuname").click(function(e){
   e.stopPropagation();
})
樱A2020/03/24 15:11:57

绝对最好的答案是在类下拉菜单后放置一个表单标签

所以你的代码是

<ul class="dropdown-menu">
  <form>
    <li>
      <div class="menu-item">bla bla bla</div>
    </li>
  </form>
</ul>