第7章 自定义事件
本文最后更新于 1803 天前,其中的信息可能已经有所发展或是发生改变。

1. jQuery自定义事件之trigger事件

众所周知类似于mousedown、click、keydown等等这类型的事件都是浏览器提供的,通俗叫原生事件,这类型的事件是需要有交互行为才能被触发。

在jQuery通过on方法绑定一个原生事件

$('#elem').on('click', function() {
    alert("触发系统事件")
 });

alert需要执行的条件:必须有用户点击才可以。如果不同用户交互是否能在某一时刻自动触发该事件呢? 正常来说是不可以的,但是jQuery解决了这个问题,提供了一个trigger方法来触发浏览器事件

所以我们可以这样:

$('#elem').trigger('click');

在绑定on的事件元素上,通过trigger方法就可以调用到alert了,挺简单!

再来看看.trigger是什么?

简单来讲就是:根据绑定到匹配元素的给定的事件类型执行所有的处理程序和行为

trigger除了能够触发浏览器事件,同时还支持自定义事件,并且自定义时间还支持传递参数

$('#elem').on('Aaron', function(event,arg1,arg2) {
    alert("自触自定义时间")
 });
$('#elem').trigger('Aaron',['参数1','参数2'])

trigger触发浏览器事件与自定义事件区别?

  • 自定义事件对象,是jQuery模拟原生实现的
  • 自定义事件可以传递参数
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title></title>
    <style>
        .left div,
        .right div {
            width: 500px;
            height: 50px;
            padding: 5px;
            margin: 5px;
            float: left;
            border: 1px solid #ccc;
        }

        .left div {
            background: #bbffaa;
        }

        .right div {
            background: yellow;
        }
    </style>
    <script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
</head>
<body>
    <h2>自定义事件trigger</h2>
    <div class="left">
        <div><span></span><span>0</span>点击次数</div>
        <button>直接点击</button>
        <button>通过自定义点击</button>
    </div>
    <script type="text/javascript">
        //点击更新次数
        $("button:first").click(function(event, bottonName) {
            bottonName = bottonName || 'first';
            update($("span:first"), $("span:last"), bottonName);
        });

        //通过自定义事件调用,更新次数
        $("button:last").click(function() {
            $("button:first").trigger('click', 'last');
        });

        function update(first, last, bottonName) {
            first.text(bottonName);
            var n = parseInt(last.text(), 10);
            last.text(n + 1);
        }
    </script>
</body>
</html>

2. jQuery自定义事件之triggerHandler事件

trigger事件还有一个特性:会在DOM树上冒泡,所以如果要阻止冒泡就需要在事件处理程序中返回false或调用事件对象中的.stopPropagation() 方法可以使事件停止冒泡

trigger事件是具有触发原生与自定义能力的,但是存在一个不可避免的问题: 事件对象event无法完美的实现,毕竟一个是浏览器给的,一个是自己模拟的。尽管 .trigger() 模拟事件对象,但是它并没有完美的复制自然发生的事件,若要触发通过 jQuery 绑定的事件处理函数,而不触发原生的事件,使用.triggerHandler() 来代替

triggerHandler与trigger的用法是一样的,重点看不同之处:

  • triggerHandler不会触发浏览器的默认行为,.triggerHandler( "submit" )将不会调用表单上的.submit()
  • .trigger() 会影响所有与 jQuery 对象相匹配的元素,而 .triggerHandler() 仅影响第一个匹配到的元素
  • 使用 .triggerHandler() 触发的事件,并不会在 DOM 树中向上冒泡。 如果它们不是由目标元素直接触发的,那么它就不会进行任何处理
  • 与普通的方法返回 jQuery 对象(这样就能够使用链式用法)相反,.triggerHandler() 返回最后一个处理的事件的返回值。如果没有触发任何事件,会返回 undefined
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title></title>
    <style>
        .left div,
        .right div {
            width: 500px;
            height: 50px;
            padding: 5px;
            margin: 5px;
            /* float: left; */
            border: 1px solid #ccc;
        }

        .left div {
            background: #bbffaa;
        }

        .right div {
            background: yellow;
        }
    </style>
    <script src="https://www.imooc.com/static/lib/jquery/1.9.1/jquery.js"></script>
</head>
<body>
    <h2>自定义事件triggerHandler</h2>
    <div class="left">
        <div id="accident">
            <a>triggerHandler事件</a>
            <input type="text">
        </div>
        <button>事件冒泡,触发浏览器默认聚焦行为</button><br><br>
        <button>不会冒泡,不触发浏览器默认聚焦行为</button>
    </div>
    <script type="text/javascript">
        //给input绑定一个聚焦事件
        $("input").on("focus", function(event, title) {
            $(this).val(title)
        });

        $("#accident").on("click", function() {
            alert("trigger触发的事件会在 DOM 树中向上冒泡");
        });
        //trigger触发focus
        $("button:first").click(function() {
            $("a").trigger("click");
            $("input").trigger("focus");
        });

        //triggerHandler触发focus
        $("button:last").click(function() {
            $("a").triggerHandler("click");
            $("input").triggerHandler("focus", "没有触发默认聚焦事件");
        });
    </script>
</body>
</html>
标题:第7章 自定义事件
地址:https://xiaodongxier.com/85.html
作者:王永杰
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇