Delay a link click

て烟熏妆下的殇ゞ 提交于 2019-11-30 18:23:20

问题


Here's the fiddle I'm working with: http://jsfiddle.net/Scd9b/

How can I delay the href function after the click?

For example a user clicks on the link, the message slides down One moment... and after 2 seconds the user continues to the page its linked to.

Sorry everybody forgot to mention there are some anchors that are not linked.


回答1:


You can simulate navigating to a page by settings window.location. So we will block the normal function of the link with preventDefault and then in a setTimeout, we will set the correct window.location:

https://codepen.io/anon/pen/PePLbv

$("a.question[href]").click(function(e){
    e.preventDefault();
    if (this.href) {
        var target = this.href;
        setTimeout(function(){
            window.location = target;
        }, 2000);
    }
});



回答2:


Check this fiddle: http://jsfiddle.net/C87wM/1/

Modify your toggle like this:

$("a.question[href]").click(function(){
    var self = $(this);
    self.toggleClass("active").next().slideToggle(2000, function() {
        window.location.href = self.attr('href'); // go to href after the slide animation completes
    });
    return false; // And also make sure you return false from your click handler.
});



回答3:


Cancel the click and use setTimeout to change the location.

$(document).ready(function(){

    $("span.answer").hide();

    $("a.question").click(function(e){
        $(this).toggleClass("active").next().slideToggle("slow");
        e.preventDefault();
        var loc = this.href;
        if(loc){
            window.setTimeout( function(){ window.location.href=loc; }, 2000 );
        }
    });

});



回答4:


$(document).ready(function(){

    $("span.answer").hide();

    $("a.question").click(function(e){
        e.preventDefault();
        $(this).toggleClass("active").next().slideToggle("slow");
        var Link = $(this).attr("href");
        setTimeout(function()
        {
             window.location.href = Link;
        },2000);
    });

});



回答5:


Prevent the default action of the link, first of all. Then add a timeout of 2 seconds, after which the page is redirected to the url found in the href attribute of the link.

$("a.question").click(function(e){
    e.preventDefault();
    $(this).toggleClass("active").next().slideToggle("slow");
    setTimeout(function(){
        location.href = $(this).prop("href");
    }, 2000);
});

Example.




回答6:


How about e.preventDefault in your click handler. Then do a setTimeout that takes you to your destination?

$("a.question").click(function(e){
    e.preventDefault();
    $(this).toggleClass("active").next().slideToggle("slow");
    setTimeout('window.location.href=' + $(this).attr(href), 2000);
});



回答7:


This should do it:

$("a[href]").click(function () {
    var url = this.href;
    setTimeout(function () {
        location.href = url;
    }, 2000);
    return false;
});


来源:https://stackoverflow.com/questions/8775541/delay-a-link-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!