【问题标题】:Bootstrap dropdown not closing on click elsewhere or only works onceBootstrap 下拉菜单不会在点击其他地方时关闭或只工作一次
【发布时间】:2015-09-08 09:53:48
【问题描述】:

我见过这个问题,但似乎没有人问同样的问题。我已将引导下拉菜单的默认功能更改为在内部单击时保持打开状态。但是,我有以下代码似乎确实可以满足我的需要,但是只有在非常感谢任何想法时才有效。

    $('.basket_toggle').on('click', function (event) {
    var that = $(this);
    $(this).parent().toggleClass('open');
    $(this).parent().find('.dropdown-menu').mouseleave(function() {
        $(document).bind("click", function () {
            that.parent().toggleClass('open');
            $(document).unbind( "click" );
        });
    });
});

【问题讨论】:

    标签: jquery twitter-bootstrap drop-down-menu


    【解决方案1】:
    $(document).unbind( "click" );
    

    此行从文档中取消绑定单击事件,然后不再绑定它。所以它只会工作一次。

    尝试动态添加 mouseleave 事件:

    $('.basket_toggle').on('click', function (event) {
        var that = $(this);
        $(this).parent().toggleClass('open');
        $(this).parent().find('.dropdown-menu').on("mouseleave",function() {
            $(document).bind("click", function () {
                that.parent().toggleClass('open');
                $(document).unbind( "click" );
            });
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-08
      • 2012-12-19
      相关资源
      最近更新 更多