简体   繁体   English

单击元素时,Bootstrap 下拉菜单不会关闭

[英]Bootstrap dropdown-menu doesn't get close when clicking an element

I'm firing the bootstrap dropdown menu by using the javascript function dropdown('toggle') as stated in their docs .我正在使用 javascript 函数dropdown('toggle')触发引导下拉菜单,如他们的文档中所述

Usually dropdowns would hide whenever you click outside them or you select one of their options.通常,只要您在下拉菜单外部单击或选择其中的一个选项,下拉菜单就会隐藏。

This doesn't happen when firing it through javascript.通过javascript触发它时不会发生这种情况。

In this reproduction you'll see two menus:此复制品中,您将看到两个菜单:

  • One which works as expected as its fired using the "components" trigger一种使用“组件”触发器按预期工作
  • And another one, using the right click, which doesn't work as expected.还有一个,使用右键单击,但没有按预期工作。 (it doesn't get closed on click outside or even on element click) (它不会在点击外部甚至元素点击时关闭)

I was able to "manually" get rid of the dropdown menu when clicking outside it by using the following:通过使用以下方法在下拉菜单之外单击时,我能够“手动”摆脱下拉菜单:

$('body').removeClass('open');

But I'm not quite sure why dropdown menus don't work in the same way as when you fire them by the normal procedure.但我不太确定为什么下拉菜单的工作方式与您通过正常程序启动它们时的方式不同。 And having to manually hide them doesn't seem like the best solution...并且不得不手动隐藏它们似乎不是最好的解决方案......

I got an answer from boostrap issues forum in which they explained how to deal with it: 我从 boostrap 问题论坛得到了一个答案,他们解释了如何处理它:

B. You're missing a <div class="dropdown"> around the <ul class="dropdown-menu"> B. 你在<ul class="dropdown-menu">周围缺少一个<div class="dropdown"> <ul class="dropdown-menu">

C. You're missing an element with data-toggle="dropdown" (Not explicitly documented, but followed by all the examples and related to the warning in http://getbootstrap.com/javascript/#callout-dropdowns-data-required ) C. 你缺少一个带有data-toggle="dropdown"的元素(没有明确记录,但后面是所有示例,并与http://getbootstrap.com/javascript/#callout-dropdowns-data- 中的警告相关) 必需

Here's a reproduction of the solution.这是解决方案的复制品。 (right anywhere click to see the dropdown menu) (在任意位置单击以查看下拉菜单)

HTML markup HTML 标记

<div class="wrapper">
    <span data-toggle="dropdown"></span>
    <ul class="dropdown-menu" id="menu">
        <li><a href="#">Download file</a></li>
        <li><a href="#">Upload file</a></li>
    </ul>
</div>

Javascript Javascript

//context menu for orders table
$(document).on("contextmenu", "body", function (event) {
    //we won't show the default context menu
    event.preventDefault();

    //showing it close to our cursor
    $('#menu').dropdown('toggle').css({
        top: (event.pageY) + "px",
        left: (event.pageX) + "px"
    });
});

Opening in Javascript does not work well with data-toggles.在 Javascript 中打开不适用于数据切换。 I once used this code to activate the dropdown from Javascript:我曾经使用此代码从 Javascript 激活下拉列表:

$(document).on("contextmenu", "body", function (event) {
    //we won't show the default context menu
    event.preventDefault();

    //showing it close to our cursor
    $('#menu').css({
        top: (event.pageY) + "px",
        left: (event.pageX) + "px"
    }).show();

    $(document).on('click.contextmenu', function () {
         $('#menu').hide();
         $(document).off('click.contextmenu');
    });
});

I have added a listener on the mouse up to close it without closing it when you click inside, so it closes just when you want or when you click outside it: http://jsfiddle.net/q6po6jzh/1/我在鼠标上添加了一个监听器来关闭它,当你在里面点击时不关闭它,所以它会在你想要或在它外面点击时关闭: http : //jsfiddle.net/q6po6jzh/1/

$(document).mousedown(function (e) {
  var container = $("#menu");

  if (!container.is(e.target) && container.has(e.target).length === 0 && container.parent().hasClass('open')) {
      container.dropdown('toggle')
      container.parent().removeClass('open');
  }
});

But if you want to be closed as well when clicked in it I guess @wero answer is probably better.但是如果你想在点击它时也被关闭,我想@wero 的答案可能会更好。

This will solve your issue.这将解决您的问题。 It will close all opened dropdowns.它将关闭所有打开的下拉菜单。

$('.in,.open').removeClass('in open');

Reference: https://stackoverflow.com/a/22632931/6488619参考: https : //stackoverflow.com/a/22632931/6488619

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM