[英]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.