我在這裏遇到了問題,我不明白爲什麼。 經過多次測試後,我明白了主要問題是Ajax請求。另外,我一直在使用另一個版本的腳本中的Ajax請求......當時我的內容變化非常順利...但是現在我想啓用後退按鈕,所以我詢問here這是如何工作的。 ..我得到了一個非常好的答案,所以我試圖做到這一點whit幫助這tutorial 現在這是下面的結果︰ 現在的網址作品的變化和舊內容正在淡出,但新內容(從請求)不更新... 更新:它的喜歡,甚至不發送請求... 有人可以告訴我爲什麼?
先進的謝謝你
對不起,我的英語不好。
的JavaScript:
$(function() {
var newHash = "",
$content = $("#content"),
$el;
$('ul li a').click(function() {
window.location.hash = $(this).attr("href");
return false;
});
$(window).bind('hashchange', function() {
newHash = window.location.hash.substring(1);
url=$(this).attr("href");
if (newHash) {
$content
.fadeOut(200, function() {
// url=url.replace('#','');
$('#loading').css('visibility', 'visible'); //show the rotating gif animation
$.ajax({
type: "POST",
url: "load_page.php",
data: 'page' + url,
success: function (msg) {
if (parseInt(msg) != 0) //if no errors
{
$('#content').html(msg); //load the returned html into pageContet
}
$('#loading').css('visibility', 'hidden'); //and hide the rotating gif
}
});
$('ul li a').removeClass("current");
$("'ul li a'[href='" + newHash + "']").addClass("current");
});
};
});
$(window).trigger('hashchange');
});
PHP:
<?php
if(empty($_POST['page'])) die("0");
$page = $_POST['page'];
// validate it as alphanumeric before reading the filesystem
if (preg_match('/^[a-z0-9]+$/i', $_POST['page']) && file_exists('article/'.$page.'.html')) {
echo file_get_contents('article/'.$page.'.html');
}
else echo 'There is no such page!';
?>
更新:在螢火蟲 - 插件我得到這個:
錯誤:語法錯誤,無法識別的表情:「UL李a [href ='anmeldung']
這是不是URL是空的?
變化'數據:「頁」 + url,'to data:'page ='+ url,' – putvande
@ adho12儘管只有一個鍵/值對,但傳遞對象並讓jQuery處理所有事情是很好的。你可以使用'data:{page:url}' – Ian
@putvande,lan對不起,他們都沒有解決這個問題......但是仍然感謝你 – adho12