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

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

问题描述

我正在使用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 across-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 various targets specified. I'd ideally like to just replay the event I stopped earlier.

我开始将事件委托与 live()一起使用,并使一切正常工作,除此之外,我将其简化为 bind()以便简化问题.

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.

推荐答案

您无法在锚点上触发click事件并使其跟随href跨浏览器.

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

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

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