jQuery自定义事件

<!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>27-jQuery事件自动触发</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

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

        .son {
            width: 100px;
            height: 100px;
            background: blue;
        }
    </style>

    <script src="js/jquery-1.12.4.js"></script>
    <script>
        $(function () {
            //     $(".son").myClick(function(){
            //         alert("son");
            //     });         

            
            /* 
                想要自定义事件,必须满足两个条件
                1. 事件必须通过on绑定
                2. 事件必须通过trigger触发
            */
            $(".son").on("myClick",function () {
                alert("son");
            });

            $(".son").trigger("myClick");
        });
    </script>
</head>

<body>
    <div class="father">
        <div class="son"></div>
    </div>
    <a href="http://www.baidu.com">我是百度</a>
    <form action="http://www.taobao.com">
        <input type="text">
        <input type="submit">
    </form>
</body>

</html>

 

posted @ 2021-05-29 17:32  胸怀丶若谷  阅读(63)  评论(0编辑  收藏  举报