如何防止CHILD元素影响PARENT元素?
jQuery:
$("#parent").click(function() {
alert('parent clicked');
});
$("#child").click(function() {
alert('child clicked');
});
HTML:
<div id="parent">
click me - parent
<div id="child">click me - child</div>
</div>
如果您单击子项,父项也会被激活,并且我们会收到两个警报。
那么如何防止 child 影响 parent 呢?
最佳答案
$("#child").click(function(event) {
event.stopPropagation();
alert('child clicked');
});
调用 event.stopPropagation()
将阻止事件在 DOM 树中冒泡。
关于jquery - 如何避免 child 影响 parent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9266871/