2012-09-06 64 views
2

可能重複:
Jquery if conditions false then prevent default不執行的href jQuery的

我有這樣的代碼,它的運行良好,但我希望它是動態的。

$(".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引導它之前先執行淡出。

我想這是第一個

$(".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" 

回答

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

試試這個:

$(".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); 
    }); 

}); 
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"; 
    }); 
}); 
+0

謝謝!這個人正在和我一起工作。我剛剛刪除了.php結尾,因爲我希望它也有一個外部網址。再次感謝:) – EatCodePlaySleep

+0

偉大的東西!不要忘記接受你的答案! –

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) 
    }); 
}