【问题标题】:jquery click outside - $(this) stop worksjquery 点击外部 - $(this) stop 有效
【发布时间】:2015-11-27 14:17:00
【问题描述】:

我想问,我哪里有错误,它是一段代码。

按钮类显示/隐藏过滤元素,这样就ok了。但我想存档“点击外部效果” - 关闭某些身体元素点击的过滤器,而不是过滤器本身。

$('.button').on('click',function() {
        var filters = $('#filters');
        if (filters.is(':hidden')) {
            filters.show();
        } else {
            filters.hide();
        }

        // Now i want to close filter, on click on body element, not filter itself.

        // Now, this code below, closes filter, on click on body element, but now .button is not closing filter. So code below is not working...

        $(document).mouseup(function (e) {
            var container = filters;
            if (!container.is(e.target) && container.has(e.target).length === 0)  {
                container.hide();
            }
        });
    });

但是现在,$(document).mouseup(function (e) - 它可以防止 .button 关闭过滤器......

怎么了?

感谢您的建议。

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    看看这个:

    https://jsfiddle.net/t1nxwvy2/3/

    您可以使用以下命令查看点击是否在按钮内部:

    $(document).mouseup(function (e) {
        var container = $('.button');
    
        if (!container.is(e.target) && (container.has(e.target).length === 0)) {
            if (!filters.is(e.target) && (filters.has(e.target).length === 0)) {
                filters.hide();
            }
        } else filters.toggle();
    });
    

    【讨论】:

      猜你喜欢
      • 2015-07-28
      • 2011-03-22
      • 1970-01-01
      • 2023-03-11
      • 2015-08-07
      • 1970-01-01
      • 2012-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多