admin管理员组文章数量:1323157
Let's say I have the following Code:
<div id="parent">
<div id="child"></div>
</div>
Now I attach an onClick event to the parent div:
$('#parent').click(function() { ... });
Is there an easy way to stop the event from triggering when I click on the child div? I don't want to do something like
$('#child').click(function() { return false; });
because the child div could contain links...
Thanks!
Let's say I have the following Code:
<div id="parent">
<div id="child"></div>
</div>
Now I attach an onClick event to the parent div:
$('#parent').click(function() { ... });
Is there an easy way to stop the event from triggering when I click on the child div? I don't want to do something like
$('#child').click(function() { return false; });
because the child div could contain links...
Thanks!
Share asked Sep 7, 2009 at 15:18 janoliverjanoliver 7,82414 gold badges62 silver badges105 bronze badges2 Answers
Reset to default 8Check the event target.
The following code is untested.
var div = document.getElementById('parent');
jQuery(div).click(function(event) {
if (event.target !== div) {
return false;
}
});
stopPropagation
$('#child').click(function(event) {
event.stopPropagation();
//...
});
本文标签: javascriptclick event on a div should not be triggered by it39s childrenStack Overflow
版权声明:本文标题:javascript - click event on a div should not be triggered by it's children - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742140358a2422554.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论