【发布时间】:2013-01-31 13:58:33
【问题描述】:
我有两个divs,类似这样:
<div id="parent" style="width: 100%; height: 100%; background-color:red;" />
<h1>I'm the parent!</h1>
<div id="child" style="width: 300px; height: 200px; background-color:yellow;">
</h2>..and I'm the child!</h2>
</div>
</div>
另外,我有以下 JavaScript 代码:
$(document).ready(function(){
$('#parent').click(function(){
alert('Parent was clicked');
});
});
问题是,如果我点击child,就会触发事件。如何将此事件限制为仅限父级?
编辑:只是为了澄清;我希望当用户单击红色的任意位置时触发此操作。如cmets所述; h1 不是父级。 Tjirp 的答案起到了作用,并且在答案中有很多这种解决方案的工作变体。
【问题讨论】:
标签: javascript jquery dom