不要执行href jquery

时间:2012-09-06 07:52:29

标签: jquery

  

可能重复:
  Jquery if conditions false then prevent default

我有这个代码,它运行正常,但我希望它是动态的。

$(".nav a").click(function () {     
    var getid = $(this).attr('id');
    $("#background-wrapper").fadeOut("slow");
    $("#page_box").fadeOut("slow");         
    $("header").fadeOut("slow");
    $("footer").fadeOut("slow", function () {
        window.location = getid+".php";
    });
});

我希望先使用href执行淡出,然后再使用href。

我想要的是第一个

$(".nav a").click(function () {     
    var getid = $(this).attr('id');
    $("#background-wrapper").fadeOut("slow");
    $("#page_box").fadeOut("slow"); 
    $("header").fadeOut("slow");
    $("footer").fadeOut("slow");
});

然后在

后触发
.nav a = href = "link"

4 个答案:

答案 0 :(得分:3)

您需要先使用event.preventDefault()来停止链接行为,然后手动重定向用户。试试这个:

$(".nav a").click(function (e) {     
    e.preventDefault();

    var getid = $(this).attr('id');
    $("#background-wrapper").fadeOut("slow");
    $("#page_box").fadeOut("slow");         
    $("header").fadeOut("slow");
    $("footer").fadeOut("slow", function () {
        location.assign = getid + ".php";
    });
});

答案 1 :(得分:1)

试试这个:

$(".nav a").click(function (e) {     

    e.preventDefault(); /*  If this method is called, the default action of the event will not be triggered. e.g clicked anchors will not take the browser to a new URL */

    var pageUrl = this.id + ".php";

    $("#background-wrapper").fadeOut("slow");
    $("#page_box").fadeOut("slow"); 
    $("header").fadeOut("slow");
    $("footer").fadeOut("slow", function () {
        window.location.replace(pageUrl);
    });

});

答案 2 :(得分:0)

尝试一下:

$(".nav a").click(function (e) {   
    e.preventDefault();
    var $ele = $(this);
    $("#background-wrapper, #page_box, header, footer").fadeOut("slow", function () {
        window.location = $ele.attr('id')+".php";
    });
});

答案 3 :(得分:0)

也许以下内容适合您?

$(".nav a").click(function (e) {     
    e.preventDefault();
    var pageUri = $(this).attr('id') + '.php';
    $("#background-wrapper, #page_box, header, footer").fadeOut('slow', function () {
        window.replace(pageUri)
    });
}