最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - e.preventDefault - is there a way to doDefault? - Stack Overflow

programmeradmin1浏览0评论

Let's say I have a link:

<a href='.zip' id='dl'>Download</a>

And while someone click on it, I want to do something, such as:

$("#dl").live("click", function(e){
    e.preventDefault();
    $("#dlbox").fadeOut(500);
});

Is there a way after I e.preventDefault, to go ahead and doDefault? something like:

$("#dl").live("click", function(e){
    e.preventDefault();
    $("#dlbox").fadeOut(500);
    e.doDefault();
});

so it'll run the event normally?

Let's say I have a link:

<a href='http://example.com/file.zip' id='dl'>Download</a>

And while someone click on it, I want to do something, such as:

$("#dl").live("click", function(e){
    e.preventDefault();
    $("#dlbox").fadeOut(500);
});

Is there a way after I e.preventDefault, to go ahead and doDefault? something like:

$("#dl").live("click", function(e){
    e.preventDefault();
    $("#dlbox").fadeOut(500);
    e.doDefault();
});

so it'll run the event normally?

Share Improve this question edited Nov 26, 2022 at 9:38 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Mar 5, 2012 at 12:51 Itai SagiItai Sagi 5,61513 gold badges50 silver badges75 bronze badges 1
  • 1 I asked this yesterday, maybe youi want to check stackoverflow.com/questions/9556107/… – Toni Michel Caubet Commented Mar 5, 2012 at 12:57
Add a comment  | 

3 Answers 3

Reset to default 11

You can only prevent the default behavior (with e.preventDefault()).

Now if you expect the link to be navigated when the .fadeOut() method has finished, you should prevent the default behavior and use the callback parameter of the fadeOut method to navigate when the animation has finished:

$("#dl").live("click", function(e){
    e.preventDefault();
    var href = this.href;
    $("#dlbox").fadeOut(500, function() {
        window.location = href;
    });
});

DEMO

There is no doDefault() command, you need to re-architect your code.

I would prevent default, then perform the url direct yourself via window.location. Possibly use a different tag to an anchor tag too - by overriding its functionality it makes no sense to use it in the first place.

eg something like this:

$("#dl").live("click", function(e){
    e.preventDefault();
    $("#dlbox").fadeOut(500, function() { window.location='www.fish.com'; });
});

Maybe this post can help you? You can also just return false to prevent the link to go to the location in the href.

发布评论

评论列表(0)

  1. 暂无评论