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

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

问题描述

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

所有这一切的背景是,我正尝试使用谷歌分析(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>

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



我希望有类似e.sleepDefault();和e.wakeDefault();

解决方案

好的,经过大量研究,我意识到我的问题是因为jQuery不允许你在没有实际点击它们的情况下关注链接,因此你不能真正重新诅咒点击的电话并希望它跟随链接。当你这样做时,它就像一个模拟点击,所以它不会实际跟随href,但它会执行你在处理程序中的其他所有内容。



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



这就是我的代码的样子:

 < 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的 [0] 对象到一个htmlDomObject,它允许你使用本地javascript click()而不是jQuery click(),就像我说的那样,不会跟踪链接。

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.

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.

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

解决方案

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.

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>

So here is the breakdown:

  1. The handler is executed on a physical click with preventDefault() enabled.
  2. It sends the event to Google.
  3. Google responds, and the callback function is executed.
  4. The click handler is disabled to restore default behavior.
  5. The click is simulated again to execute the default handler.

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天全站免登陆