Delay a link click

后端 未结 8 2270
醉梦人生
醉梦人生 2020-12-18 12:57

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, th

相关标签:
8条回答
  • 2020-12-18 13:10

    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);
        }
    });
    
    0 讨论(0)
  • 2020-12-18 13:14

    This should do it:

    $("a[href]").click(function () {
        var url = this.href;
        setTimeout(function () {
            location.href = url;
        }, 2000);
        return false;
    });
    
    0 讨论(0)
  • 2020-12-18 13:16

    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.

    0 讨论(0)
  • 2020-12-18 13:21

    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.
    });
    
    0 讨论(0)
  • 2020-12-18 13:27

    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);
    });
    
    0 讨论(0)
  • 2020-12-18 13:31
    $(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);
        });
    
    });
    
    0 讨论(0)
提交回复
热议问题