可能是重复问题:
防止点击事件影响父级jQuery
我有一个类似于以下结构的DOM:
<div class="parent">
<div class="child">
</div>
</div>
I have the following jQuery:
$('.parent').click(function(){
$(this).hide();
});
当单击.child
时,我不希望父元素隐藏,但显然由于.child
是.parent
的子元素,这种情况自然会发生。我尝试添加以下内容:
$('.child').click(function(e){
e.stopPropagation();
});
但是它仍然不起作用。我也尝试将
e.stopPropagation
替换为return false;
有什么建议吗?
JSFiddle