有没有办法防止默认事件,然后用jQuery再次激活它?

有没有办法防止默认事件,然后用jQuery再次激活它?

问题描述:

基本上我有一个锚元素,< a href ='bla ..'> link< / a>

Basically I have an anchor element, <a href='bla..'>link</a>

点击后,我首先要做的事情,只有在完成后我才想让用户访问它链接到的页面。类似于:

On click, I first want to do something and only once it's done I want to take the user to the page that it links to. Something like:

$('a').click(function(ev){
   ev.preventDefault();

   //do something here

   ev.refireDefault();
});

有任何建议吗?

我的道歉!我的FireFox决定缓存以前版本的JS,所以我尝试过的只是一个简单的CTRL + F5解决了这个问题。

My apologies! My FireFox decided to cache the previous version of JS, so nothing that I tried worked until a simple CTRL+F5 solved the issue.

Javascript不是多线程的。它是事件驱动的,事件按照加载它们的顺序触发。因此,如果您只是完全忽略了单击事件中的 ev.preventDefault(),则在函数退出之前不会触发默认操作。

Javascript is not multi-threaded. It is event driven and events fire in the order in which you load them. Therefore, if you simply leave out the ev.preventDefault() in your click event altogether, it won't fire the default action until the function exits.

在这里查看jsFiddle

按照@ Greg的评论编辑:

上述情况不正确的唯一情况是异步函数。在这种情况下,您可能希望首先阻止默认操作,然后在异步回调函数中重新触发默认操作。

The only scenario in which the above is not true is with asynchronous functions. In that scenario you would want to prevent the default action first and then in the asynchronous callback function, re-fire the default action.