js事件冒泡--通俗易懂

①父元素的事件阻止子元素触发,父元素绑定完事件后return false
②子元素事件防止父元素触发,子元素需添加stopPropagation()
下面是自己写的小案例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
    <title>Document</title>
    <style>
        #one{width:400px;height:400px;background-color: red;}
        #two{width:200px;height:200px;background-color: green;}
        #three{width:50px;height:50px;background-color: blue;}
    </style>
</head>
<body>
    <div id="one">
        一区
        <div style="background-color: yellow;width:300px;height:300px;" id="test">子元素群
        <div id="two">二区
            <div id="three">三区</div>
        </div>
        </div>
    </div>
    <script>
        $("#one").click(function(e){
            console.log("one")
            return false
        })
        $("#two").click(function(e){
            console.log("two")
            return false
        })
        $("#three").click(function(e){
            e.stopPropagation()
            console.log("three")
        })
    </script>
</body>
</html>

以前傻乎乎的以为父元素对孙子曾孙子会阻止事件,其实在父元素中,子元素主动e.stopPropagation(),该子元素才不会受父元素的事件影响

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值