2013-08-22 116 views
0

我在這裏遇到了問題,我不明白爲什麼。 經過多次測試後,我明白了主要問題是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']

This is how my FireBug Consol looks like...這是不是URL是空的?

+1

變化'數據:「頁」 + url,'to data:'page ='+ url,' – putvande

+1

@ adho12儘管只有一個鍵/值對,但傳遞對象並讓jQuery處理所有事情是很好的。你可以使用'data:{page:url}' – Ian

+0

@putvande,lan對不起,他們都沒有解決這個問題......但是仍然感謝你 – adho12

回答

0
$(function() { 

    var newHash  = "", 
     $content = $("#content"); 



    $('ul li a').click(function() { 
     window.location.hash = $(this).attr("href"); 
     return false; 
    }); 

    $(window).bind('hashchange', function(){ 

     newHash = window.location.hash.substring(1); 
     url=newHash 
     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.fadeIn(200, function() { 
      $('#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'); 

}); 

唉唉,我發現了錯誤 的問題是,我設置的URL已晚... 感謝每個人 - 機器人誰看了一下,幫我解決了這個^^

1

您正在以錯誤的方式將數據發送到您的PHP腳本。 你應該發送它爲key=value,你發送它像keyvalue,所以你的PHP腳本永遠不會得到page變量。

所以從

data: 'page'+url, 

data: 'page='+url, 
//  ^
+0

對不起,這並沒有解決問題,但仍然謝謝你 – adho12

2

更改此行改變你的腳本:

$("'ul li a'[href='" + newHash + "']").addClass("current"); 

進入這一個:

$('ul li a[href="' + newHash + '"]').addClass('current'); 

至少這就是 FireBug抱怨的語法錯誤。