jQuery $('html,body').not()

Ada*_*chy 5 jquery click

在这里 - 当你点击任何地方而不是在div上时,我希望发生警报.

当我点击div时,警报也会显示.

JS

$("html, body").not('.entry-content').click(function() {            
    alert('d');                        
}); ?
Run Code Online (Sandbox Code Playgroud)

HTML

<div class="entry-content"> kkkk </div>?
Run Code Online (Sandbox Code Playgroud)

ᾠῗᵲ*_*ᵲᄐᶌ 17

您可以使用event参数查看单击的目标并返回false

$("html, body").click(function(e) {
    if ($(e.target).hasClass('entry-content')) {
        return false;
    }
    alert('d');
});?
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/keyZw/

您正在使用.not()过滤器..但它仍然是您的html/body的一部分..因此您需要在click函数内处理它.你也只是绑定点击事件..

所以

 // find html,body - not ones with class=entry-content - bind click
 $("html, body").not('.entry-content')
Run Code Online (Sandbox Code Playgroud)

这样就不会阻止警报,因为你的div仍在体内

如上所述..你只需要真正绑定到身体

$("body").click(function(e) {
    if ($(e.target).hasClass('entry-content')) {
        return false;
    }
    alert('d');
});?
Run Code Online (Sandbox Code Playgroud)


Zol*_*oth 7

$("html *").click(function(e) {
    if ( e.target.className.indexOf("entry-content") < 0 ) {
        alert('d');
    }
}); 
Run Code Online (Sandbox Code Playgroud)

DEMO


原始代码不起作用,因为它.not()适用于它前面的选择器 - htmlbody.他们都没有这类entry-content,所以事件发生了