jQuery - 我可以在它自己的处理程序中触发一个事件吗? [英] jQuery - Can I trigger an event within it's own handler?

查看:36
本文介绍了jQuery - 我可以在它自己的处理程序中触发一个事件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我基本上有一个 .click 事件,它执行 e.preventDefault().但是,一旦代码完成执行,我希望它在解除绑定后重新执行该事件.

So I basically have a .click event, that does e.preventDefault(). However, once the code finishes executing, I want it to re-execute the event after unbinding it.

所有这一切的背景是,我正在尝试使用谷歌分析 (analytics.js) 设置出站链接事件跟踪.

The context for all of this is, I'm trying to set up outbound link event tracking with google analytics (analytics.js).

<a href="http://www.example.com/" id="link1"><img /></a>    
<script>        
    $("#link1").click(function(e) {
        e.preventDefault();

        ga('send', 'event', 
            'category', 
            'action', 
            'label', 
            { 
                'hitCallback': function() {                                         
                    $(this).unbind(e).trigger("click");
                } 
            }
        );
    });
</script>

此设置是必要的,否则页面在发送事件之前终止.另外,我想让代码片段尽可能独立,因为这个脚本会被复制粘贴到很多地方.

This setup is necessary, otherwise the page terminates before the event can be sent. Also, I would like to make the snippet as independent as possible, since this script will be copy-pasted to a lot of places.

我希望有 e.sleepDefault() 之类的东西;和 e.wakeDefault();

I wish there was something like e.sleepDefault(); and e.wakeDefault();

推荐答案

好吧,经过大量研究,我意识到我的问题特别在于jQuery 不允许您在没有实际实际点击链接的情况下跟踪链接,因此,您不能真正重新诅咒点击调用并希望它遵循链接.当您这样做时,它充当模拟"单击,因此它实际上不会跟随 href,但它会执行您在处理程序中拥有的所有其他内容.

Ok, after a lot of research, I realized that my problem was specifically with the fact that jQuery will not allow you to follow links without actually physically clicking them, hence you can't really re-curse a call to click and hope that it follows the link. When you do that, it acts as a "simulated" click, so it won't actually follow the href, but it will execute everything else you have in the handlers.

解决方案是使用 javascript 的原生 click(),它不关心并且会跟随链接.

The solution, was to use javascript's native click(), which doesn't care and will follow the link.

这就是我的代码的样子:

So this is what my code looked like:

<a href="http://www.example.com/" id="link1"></a>    
<script>        
    $("#link1").click(function(e) {
        var obj = $(this);
        e.preventDefault();

        ga('send', 'event', 
            'category', 
            'action', 
            'label', 
            { 
                'hitCallback': function() {                                         
                    obj.off(e);
                    obj[0].click();
                } 
            }
        );
    });
</script>

所以这里是细分:

  1. 在启用 preventDefault() 的物理点击时执行处理程序.
  2. 它将事件发送给 Google.
  3. Google 响应,并执行回调函数.
  4. 禁用点击处理程序以恢复默认行为.
  5. 再次模拟点击以执行默认处理程序.

关键是将 jQuery 对象转换为 htmlDomObject 的 [0] ,它允许您使用本机 javascript click() 而不是 jQuery click() ,就像我一样说,不遵循链接.

The key is the [0] that converts the jQuery Object to an htmlDomObject which allows you to use the native javascript click() instead of the jQuery click() which, like I said, DOES NOT FOLLOW LINKS.

这篇关于jQuery - 我可以在它自己的处理程序中触发一个事件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆