木子屋 Dnawo's BLOG

jQuery自定义事件添加和触发示例

👤 dnawo 📅 2019-11-23 👁 2498 👍 0 💬 0 🔄 来源:本站原创
我们可以用jQuery的on方法在任一对象上添加自定义事件,之后用trigger方法触发自定义事件,代码示例:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>jQuery自定义事件添加触发示例</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="//code.jquery.com/jquery-latest.js"></script>
    </head>

    <body>
        <button>Button #1</button>
        <script>
            //1.添加自定义事件
            $(document).on("event_mzwu_com", "button", function(event, param1, param2) {
                alert(param1 + "\n" + param2);
            });
            //2.触发自定义事件
            $("button").trigger("event_mzwu_com", ["Custom", "Event"]);
        </script>
    </body>
</html>

评论(0)

暂无评论。

验证码
评论需审核通过后显示
← 上一篇 Bootstrap Table服务器端分页使用示例 下一篇 → Chrome无痕模式和Firefox隐私窗口启动参数