简体   繁体   English

单击背景如何关闭弹出窗口

[英]How close popup by click on background

i have simple dropdown, and i need to hide (fadeout) it by clicking on the document 我有一个简单的下拉菜单,我需要通过单击文档将其隐藏(淡出)

在此处输入图片说明

this is my jquery code: 这是我的jQuery代码:

var expandCheckbox = $('.filterShow'),
    formCheckbox = $('.checkboxWrap');
expandCheckbox.click(function(e) {
    e.preventDefault();

    if ($(this).hasClass('active')) {
        $(this).removeClass('active');
        $(formCheckbox).fadeOut(200);
    } else {
        $(this).addClass('active');
        $(formCheckbox).fadeIn(100);
    }
});

$('body').not('.filterShow, .checkboxWrap').click(function() {
    $(formCheckbox).fadeOut(100);
});

SEE JSFIDDLE 查看JSFIDDLE

Remove "e.preventDefault" which has no use here, add "return false" to prevent event propagation to its parent, use "document" instead of "body" as the selector for fading the dropdownlist upon clicking anywhere on the document. 删除此处未使用的“ e.preventDefault”,添加“ return false”以防止事件传播到其父级,单击“文档”而不是“正文”作为选择器,以在单击文档中的任何位置时使下拉列表褪色。

$(document).ready(function(){
    expandCheckbox = $('.filterShow'),
    formCheckbox = $('.checkboxWrap');
    expandCheckbox.click(function (e) {

        if ($(this).hasClass('active')) {
            $(this).removeClass('active');
            formCheckbox.fadeOut(200);
        } else {
            $(this).addClass('active');
            formCheckbox.fadeIn(100);
        }
        return false;
    });

    $(document).click(function (e) {
        if (expandCheckbox.hasClass('active')) {
            expandCheckbox.removeClass('active');
            formCheckbox.fadeOut(200);
        }
        return false;
    });

    formCheckbox.find('input:checkbox').click(function (e) {
        e.stopPropagation();
    });
});

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

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