JQuery 关闭事件冒泡

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>event_stop_propagation_事件_停止_传播</title>
    <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
    <script type="text/javascript">
        $(function(){

            //event 是自带对象, 需要使用的时候,通过function()第一个传入.
            $('.son').click(function(event){
                alert(1);

                event.stopPropagation();
                // 使用event对象中的stopPropagation(停止传播),可以关闭事件冒泡.

                // event.stopDefault();
                // 关闭默认行为

                // 通常.event.stopPropagation 与 event.stopDefault
                // 连写为 return false;

            })
            $('.father').click(function(){
                alert(2)
            })
            $('.grandfather').click(function(){
                alert(3)
                return false;
            })

            $(document).click(function(){
                alert(4)
            })



        })


    </script>
    <style type="text/css">
        
        .grandfather{
            width: 300px;
            height: 300px;
            position: relative;
            background-color: tan;
        }
        .father{
            width: 200px;
            height: 200px;
            background-color: cyan;
        }
        .son{
            width: 100px;
            height: 100px;
            background-color: violet;
            position: absolute;
            top: 350px;
        }


    </style>
</head>
<body>
    <div class="grandfather">
        <div class="father">
            <div class="son"></div>
        </div>
    </div>
</body>
</html>

 

转载于:https://www.cnblogs.com/jrri/p/11348345.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值