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
Add a ment  | 

5 Answers 5

Reset to default 3

Try

$(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
Add a ment  | 

5 Answers 5

Reset to default 3

Try

$(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