jQery基础,事件冒泡个默认行为stopImmediatePropagation(); preventDefault();

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .father {
            width: 200px;
            height: 200px;
            background: red;
        }

        .son {
            width: 100px;
            height: 100px;
            background: blue;
        }
    </style>
    <script src="../jquery-3.3.1.js"></script>
    <script>
        $(function () {
            // $(".son").click(function (event) {
            //     alert("son");
            //     // return false;
            //     event.stopImmediatePropagation();
            // });
            // $(".father").click(function () {
            //     alert("father");
            // });


            //给a标签添加事件
            $("a").click(function(event){
                alert("弹出注册框");
                // return false;
                //event.stopImmediatePropagation();点击后回跳转
                event.preventDefault();//点击后不会跳转
            })
        })

    </script>
</head>

<body>
    <div class="father">
        <div class="son"></div>
    </div>
    <a href="https://www.bilibili.com/">bilibili干杯!</a>
    <form action="http://www.taobao.com">
        <input type="text">
        <input type="submit">
    </form>
</body>

</html>
event.stopImmediatePropagation();点击后回跳转
event.preventDefault();//点击后不会跳转
上一篇:列举几个常见的浏览器兼容问题


下一篇:2021-2-14