admin管理员组文章数量:1022935
jQuery
$(document).click( function () {
alert("hi");
}
HTML
<div id="wrapper">
<div id="inner-1">
<div id="sub-inner1">Sub Inner1 Content</div>
<div id="sub-inner2">Sub Inner2 Content</div>
<div id="sub-inner3">Sub Inner3 Content</div>
</div>
<div id="inner-2" style="float:left; width:49%; border:dotted #CC3300;">
content inside inner-2 div
</div>
</div>
I want to show the Hi alert message on the Document click using Jquery. But I want to exclude the Inner-1 div and also its children. I meant that I want to get the alert message only when I click on the divs other than inner-1 and its children. Thanks.
jQuery
$(document).click( function () {
alert("hi");
}
HTML
<div id="wrapper">
<div id="inner-1">
<div id="sub-inner1">Sub Inner1 Content</div>
<div id="sub-inner2">Sub Inner2 Content</div>
<div id="sub-inner3">Sub Inner3 Content</div>
</div>
<div id="inner-2" style="float:left; width:49%; border:dotted #CC3300;">
content inside inner-2 div
</div>
</div>
I want to show the Hi alert message on the Document click using Jquery. But I want to exclude the Inner-1 div and also its children. I meant that I want to get the alert message only when I click on the divs other than inner-1 and its children. Thanks.
Share Improve this question edited Oct 15, 2013 at 6:37 Tepken Vannkorn 9,72314 gold badges62 silver badges86 bronze badges asked Oct 15, 2013 at 5:54 PearlPearl 9,4558 gold badges45 silver badges60 bronze badges 1-
How about
$('#inner-1').on('click', function() { return false; });
? – benastan Commented Oct 15, 2013 at 6:39
5 Answers
Reset to default 3Try
$(document).on('click', function (e) {
if($(e.target).closest('#inner-1').length == 0){
console.log("click", e.target);
}
});
Demo: Fiddle
Simply bind a new event to excluding element like this:
jQuery(document).on('click', function(e) {
alert('Document got a click event');
});
// Here bind event on those event on which you want to exclude from
// document click
jQuery(document).on('click', '#inner-1', function(e) {
e.preventDefault();
e.stopPropagation();
return false;
});
Try this:
$('#wrapper>div').not('#inner-1').click( function () {
alert("hi");
});
Demo here
Please check out this one.
$(document).on("click", "#wrapper > div[id!='inner-1']", function(){
alert("Hi");
});
I hope this will help you.
$(document).not('#inner-1').click(function () {
alert("hi");
});
reference .not()
jQuery
$(document).click( function () {
alert("hi");
}
HTML
<div id="wrapper">
<div id="inner-1">
<div id="sub-inner1">Sub Inner1 Content</div>
<div id="sub-inner2">Sub Inner2 Content</div>
<div id="sub-inner3">Sub Inner3 Content</div>
</div>
<div id="inner-2" style="float:left; width:49%; border:dotted #CC3300;">
content inside inner-2 div
</div>
</div>
I want to show the Hi alert message on the Document click using Jquery. But I want to exclude the Inner-1 div and also its children. I meant that I want to get the alert message only when I click on the divs other than inner-1 and its children. Thanks.
jQuery
$(document).click( function () {
alert("hi");
}
HTML
<div id="wrapper">
<div id="inner-1">
<div id="sub-inner1">Sub Inner1 Content</div>
<div id="sub-inner2">Sub Inner2 Content</div>
<div id="sub-inner3">Sub Inner3 Content</div>
</div>
<div id="inner-2" style="float:left; width:49%; border:dotted #CC3300;">
content inside inner-2 div
</div>
</div>
I want to show the Hi alert message on the Document click using Jquery. But I want to exclude the Inner-1 div and also its children. I meant that I want to get the alert message only when I click on the divs other than inner-1 and its children. Thanks.
Share Improve this question edited Oct 15, 2013 at 6:37 Tepken Vannkorn 9,72314 gold badges62 silver badges86 bronze badges asked Oct 15, 2013 at 5:54 PearlPearl 9,4558 gold badges45 silver badges60 bronze badges 1-
How about
$('#inner-1').on('click', function() { return false; });
? – benastan Commented Oct 15, 2013 at 6:39
5 Answers
Reset to default 3Try
$(document).on('click', function (e) {
if($(e.target).closest('#inner-1').length == 0){
console.log("click", e.target);
}
});
Demo: Fiddle
Simply bind a new event to excluding element like this:
jQuery(document).on('click', function(e) {
alert('Document got a click event');
});
// Here bind event on those event on which you want to exclude from
// document click
jQuery(document).on('click', '#inner-1', function(e) {
e.preventDefault();
e.stopPropagation();
return false;
});
Try this:
$('#wrapper>div').not('#inner-1').click( function () {
alert("hi");
});
Demo here
Please check out this one.
$(document).on("click", "#wrapper > div[id!='inner-1']", function(){
alert("Hi");
});
I hope this will help you.
$(document).not('#inner-1').click(function () {
alert("hi");
});
reference .not()
本文标签: javascriptHow to exclude the elements on Document clickStack Overflow
版权声明:本文标题:javascript - How to exclude the elements on Document click - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745497475a2153233.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论