演示html代码:
<div class="parent">
<div class="children">
</div>
</div>
<script>
$(".parent").click(function(){
console.log("parent");
});
$(".children").click(function(){
console.log("children");
});
</script>
结果,在点击children时,会输出:children parent。这就是事件冒泡,如何阻止事件冒泡,只响应本Dom元素的click事件,只要在函数中添加return false;
<script>
$(".parent").click(function(){
console.log("parent");
});
$(".children").click(function(){
console.log("children");
return false;
});
</script>