jQuery $('html,body').not() [英] jQuery $('html, body').not()
问题描述
此处-我希望当您单击div而不是div时出现警报.
here - I want alert to happen when you click anywhere but not on the div.
当我单击div时,也会显示警报.
When I click on div, alerts shows too.
JS
JS
$("html, body").not('.entry-content').click(function() {
alert('d');
});
HTML
HTML
<div class="entry-content"> kkkk </div>
推荐答案
您可以使用事件参数查看单击了哪个目标并返回false
You can use the event argument to see what target was clicked and return false
$("html, body").click(function(e) {
if ($(e.target).hasClass('entry-content')) {
return false;
}
alert('d');
});
您正在使用.not()过滤器..,但它仍然是html/body的一部分.因此,您需要在click函数中进行处理.另外,您只是绑定click事件.
You are using the .not() filter.. but it's still part of your html/body.. so you need to handle it inside the click function. Also you are just binding the click event..
所以
// find html,body - not ones with class=entry-content - bind click
$("html, body").not('.entry-content')
这不会阻止警报,因为您的div仍在体内
So that doesn't prevent the alert as your div is still inside the body
如上所述,您只需要真正地绑定到身体
As mentioned.. you only need to bind to the body really
$("body").click(function(e) {
if ($(e.target).hasClass('entry-content')) {
return false;
}
alert('d');
});
这篇关于jQuery $('html,body').not()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!