javascript如何稍后触发本机点击操作(重放事件)? [英] javascript How to trigger native click action (replay event) later?

查看:300
本文介绍了javascript如何稍后触发本机点击操作(重放事件)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery绑定到页面上的所有链接(我使用'click'事件,但尝试了'mousedown'和'mouseup'的各种组合,以及bind()和live()无效)。

I'm using jQuery to bind to all links on the page (I'm using the 'click' event, but have tried various combinations of 'mousedown' and 'mouseup',together with bind() and live() to no avail).

我可以拦截点击没有问题(与所有上述方法)。我想要做的是通过GET请求发送一些数据,当它完成后,允许默认点击操作继续。

I can intercept the click no problem (with all the above methods). What I am trying to do is send some data via a GET request, and when it completes, allow the default click action to proceed.

,我必须使用GET而不是POST,因此不能进行同步调用。

Since I am communicating accross-domain, I must use GET rather than POST, and so cannot make a synchronous call.

因此,我必须从拦截的click事件返回'false',存储事件稍后,一旦通信完成,再次手动启动它。如果我返回true,当页面位置改变时,通信中断。

Therefore I have to return 'false' from the intercepted click event, store the event for later, then manually fire it again once the communication has completed. If I return true, the communication gets cut off mid-way as the page location changes.

问题是,我找不到一个方法来激活本地点击事件后。

The problem is, I can't find a way to fire the native click event later on.

var storedEvent;
$("#wrapper a").bind('click', function(event, processed) {
    $(event.target).unbind('click'); // temporary to make code branching easier
    storedEvent = event.target;
    event.stopPropagation();

    $.ajax({
        dataType: 'jsonp',
        data: linkData,
        jsonp: 'cb',
        url: 'xxx',
        cache: false,
        complete: function(response) {
            // How do I now go back and fire the native click event here?
            $(storedEvent).click();
        }
    });
    return false;
}

我尝试过使用click()和trigger()指示,但都不工作。

I've tried using click() and trigger() where indicated, but neither worked.

我知道提交是成功的,代码正确分支 - 我已经调试了远,我似乎不能重放事件。

I know the submission is succeeding, and the code is branching correctly -- I have debugged that far. I just don't seem to be able to replay the event.

注意,我不能做一些简单的事情,像存储href和设置window.location稍后 - 一些链接有自己的onClicks设置,而其他人有指定变量目标。我最好只是重播我之前停止的事件。

Note that I can't do something simple, like store the href and set window.location later -- some of the links have their own onClicks set, while others have varius targets specified. I'd ideally like to just replay the event I stopped earlier.

我开始使用事件委托与live()和一切工作,除了这 - 我

I started off using event delegation with live() and had everything working, apart from this -- I have simplified it down to a bind() in order to simplify the problem.

推荐答案

您不能在锚点上触发点击事件并让它按照href跨浏览器。

You cannot trigger a click event on an anchor and get it to follow the href cross browser.

这篇关于javascript如何稍后触发本机点击操作(重放事件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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