如果单击文档,我试图触发点击事件,除非点击我拥有某个类名的div。
我尝试了以下但事件不会发生。
$(document).not(".class").click(function(){
//code
});
$(document).not("div.class").click(function(){
//code
});
$("body").not(".class").click(function(){
//code
});
任何人都知道我做错了什么?
由于
*更新
我尝试了以下
$(document).on("click", ":not(.class)", function(){
$('*:not(.class)',document).click(function(){
$(document).find(':not(.class)').click(function(){
$("body:not(.thisclass)").doAction();
但它没有对我试图排除的.class的div给出任何区别。任何单击文档时都会触发该事件。
这是我的html中的div
<div class="pictureBox" id="pictureBox2"> <!--content--></div>
答案 0 :(得分:8)
您正在将点击处理程序附加到document
本身,因此它将始终触发(除非其他处理程序阻止它)。
$(document).on("click", ":not(.class)", function(){
//code
});
这仍然附加document
上的事件处理程序,但此处理程序仅响应其源与:not(.class)
选择器匹配的点击:当事件bubble up到document
jQuery调用时仅当源与选择器匹配时才处理。
答案 1 :(得分:1)
问题在于,您的选择器会尝试定位document
或body
而不类class
而不是class
元素,你可以尝试类似的东西:
$('*:not(.class)',document).click(function(){
或者:
$(document).find(':not(.class)').click(function(){
答案 2 :(得分:1)
$("body:not(#thisid)").doAction();
$("body:not(#thisid.thisclass)").doAction();
$("body:not(.thisclass)").doAction();