javascript - 使用 :not to select all elements except inside box with certain class

标签 javascript jquery css jquery-selectors

我有一个叠加层,我想在它外面的 mousedown 上隐藏它。

这是我尝试过的,但不幸的是 :not 选择器没有像我预期的那样工作。

$('body:not(.overlay)').one('mousedown',function(){
        //hide overlay here
});

我也试过 $('* :not(.overlay)') 但同样的问题。

即使在叠加框内单击,叠加层也会隐藏

最佳答案

$(document).on( "mousedown.hideoverlay", function(e){
    if( $(e.target).closest(".overlay").length === 0 ) { //overlay wasn't clicked.
        //hide overlay
        $(document).off("mousedown.hideoverlay");
    }
});

关于javascript - 使用 :not to select all elements except inside box with certain class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11289425/

相关文章:

javascript - 仅 AJAX 动态纸质标签

javascript - 使用 amcharts 创建百分比

javascript - Grunt uncss 忽略 javascript 生成的 css

javascript - PHP双引号和单引号引起的麻烦

javascript - 将事件发送到 GA 的正确方法是什么?

css - 如何使用纯 CSS 创建此箭头

html - CSS - 溢出 : hidden to not cover dropdown

Javascript/Angular : wait for promise to be fulfilled before going to the next iteration in foreach Loop

javascript - 在另一个下方显示搜索建议

javascript - 如何在单击jquery时交换图像