<jQuery> <方法> 十八. 移除事件, 触发事件, 事件对象(阻止冒泡, 阻止跳转)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p>RayLee</p>
<p>RayLee</p>
<p>RayLee</p>
<input type="button" value="触发" id="btn">
<input type="button" value="事件对象" id="btn1">
<a href="http://www.baidu.com" id="link">跳转</a>

<script src="jquery-3.2.1.js"></script>
<script>
    $(function () {
        // 注册事件
        $("p").on("click", function () {
            alert("hehe");
        });

        // 移除所有事件
        // $("p").off();

        // 触发事件
        // $("#btn").click(function () {
        //     $("p").click();
        //     $("p").trigger("click");
        // })

        // 事件对象, e就是事件对象
        $("#btn1").on("click", function (e) {
            console.log(e);
        });

        // 阻止冒泡和阻止浏览器的默认行为
        $("#link").on("click", function (e) {
            alert("aaa");
            // 阻止跳转
            e.preventDefault();
            // 阻止冒泡
            e.stopPropagation();
        })

        $("#link").on("click", function (e) {
            alert("bbb");
        });
    });
</script>
</body>
</html>

 

posted @ 2018-01-13 11:15  云淡风轻我爱编程  阅读(189)  评论(0编辑  收藏  举报