简体   繁体   English

如何检测元素和其中一个元素以外的所有元素的点击?

[英]how can I detect a click on an element and all the elements inside except one?

I am trying to click on a div and trigger a function (in javascript/jquery) but dont triger it if I click a child element of that div. 我试图单击一个div并触发一个函数(在javascript / jquery中),但是如果我单击该div的子元素,则不要触发它。

Well, basically, I have this structure 好吧,基本上,我有这个结构

<div class="parent">
   <div class="child_1">
      <div class="child_2">
         <button id="myButton"></button>
      </div>
   </div>
</div>

What I want to do, is that I want to detect if I click on .parent and triger a function, but dont triger it if i click on #myButton 我想做的事情,是我想如果我点击检测.parent和TRIGER功能,但不通过靶向它,如果我点击#myButton

so far, Ive tried many ways but I cannot figure out the proper way, if I have a 到目前为止,我已经尝试了许多方法,但是如果我有

$(".parent").click(function(){...})

seems to call it as well if I click on myButton, any ideas? 如果我单击myButton,似乎也可以调用它,有什么想法吗?

Check the event target: 检查event目标:

$(".parent").click(function(e){
    if (e.target.id == "myButton") return false;

    //code
})

Le fiddle: http://jsfiddle.net/rz6oqbh0/ 小提琴: http : //jsfiddle.net/rz6oqbh0/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何选择DOM中除一个元素外的所有元素? - How to select all the elements in the DOM except one element? 如何从一组元素中删除所有边框(除了创建的边框以外)? - How can I remove all borders from a group of elements except for the one created? 我怎样才能使所有的元素<p>除了具有正确 id 的标签外,标签不可见 - How can I make all elements of the <p> tag invisible except for the one with the correct id 我怎么能删除所有 <tr> HTML表格中的元素(第一个除外) - How can I delete all the <tr> elements (except the first one) in a HTML table 如何解析文档并删除除匹配和ID及其子项之外的所有元素 - How can I parse a document and remove all elements except for one that matches and ID and its children jQuery mobile-如何折叠除我单击的以外的所有内容 - Jquery mobile - How to collapse all except one that i click 我如何覆盖div中除一个元素之外的所有内容 - How do i overwrite everything inside a div except for one element 当事件设置为外部元素时,如何检测另一个元素内的元素? - How can I detect the element inside another element while the event is set to the outer one? 如何更改除clicked元素之外的所有元素? - How to change all elements except clicked element? 敲除单击绑定-在除clicked元素之外的所有元素上阻止绑定 - knockout click binding - block binding on all elements except clicked element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM