2016-11-25 147 views
0

我繼續得到'沒有這樣的頁面!'消息隨時我嘗試使用按鈕來更改內容。
這裏是的index.htmlAJAX/PHP/JS - 無法將頁面內容加載到容器中

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>eWorld</title> 
<link rel="stylesheet" type="text/css" href="/css/style.css" /> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
<script type="text/javascript" src="js/script.js"></script> 

</head> 

<body> 

<div id="rounded"> 
<div id="main" class="container"> 
<h1>eWORLD</h1> 
<h2>You make difference.</h2> 

<ul id="navigation"> 
<li><a href="#forum">Forum</a></li> 
<li><a href="#about">About</a></li> 
<li><a href="#register">Register</a></li> 
<li><a href="#login" target=pageContent>Login</a></li> 
<li><img id="loading" src="img/ajax_load.gif" alt="loading" /></li> 
</ul> 
</div> 
<div class="clear"></div> 
</div> 
<div class="clear"></div> 
<div id="pageContent"> 
</div> 
<div class="clear"></div></div> 
<div align="center" class="#footer">eWorld</div> 

</body> 
</html> 

的script.js

var default_content=""; 
$(document).ready(function(){ 

checkURL(); 
$('ul li a').click(function (e){ 

     checkURL(this.hash); 

}); 

default_content = $('#pageContent').html(); 


setInterval("checkURL()",250); 

}); 

var lasturl=""; 

function checkURL(hash) 
{ 
if(!hash) hash=window.location.hash; 

if(hash != lasturl) 
{ 
    lasturl=hash; 

    if(hash=="") 
    $('#pageContent').html(default_content); 

    else 
    loadPage(hash); 
} 
} 


function loadPage(url) 
{ 
url=url.replace('#page',''); 

$('#loading').css('visibility','visible'); 

$.ajax({ 
    type: "POST", 
    url: "load_page.php", 
    data: 'page='+url, 
    dataType: "html", 
    success: function(msg){ 

     if(parseInt(msg)!=0) 
     { 
      $('#pageContent').html(msg); 
      $('#loading').css('visibility','hidden'); 
     } 
    } 

}); 

} 

此外,load_page.php

<?php 
if(!$_POST['page']) die("0"); 

$page = $_POST['page']; 

$newstr = str_replace("#","", $page); 

if (!file_exists('pages/'.$newstr.'.html')) 
echo file_get_contents('pages/'.$newstr.'.html'); 

else echo 'There is no such page!'; 
?> 

它看起來像是scrip t工作正常,但無法進入新頁面的目錄(這是/pages/forum.html; /pages/login.html等)。 將不勝感激,如果有人能告訴我去哪裏我所犯的錯誤... :)

+2

不是'if(!file_exists(...))'是指「如果文件**不存在」? – Pointy

+0

尖尖的,你明白了!問題解決了,謝謝! –

回答

1

您正試圖獲得.hash DOM元素:

$('ul li a').click(function (e){ 
    checkURL(this.hash); 

反而得到href屬性:

checkURL(this.attr("href")); 

.hash只能URL對象上

此外,該第一使用checkURL的();在documentReady應該拋出錯誤,因爲它與方法定義不匹配

+0

現在它不會識別this.attr作爲函數,錯誤如下:TypeError:this.attr不是函數 –

+0

試試這種方式:checkURL($(this).attr('href')) - attr is一個jQuery的功能,所以它可能需要一個jQuery選擇器。對不起,JS內部工作有些生疏 – Auris

+0

謝謝,它清除了控制檯,所以現在沒有錯誤,仍然返回'沒有這樣的頁面。' tho,我想知道如果load_page.php創建一個有效的鏈接,這可能是問題,但我不能識別這個問題的根源。 –

0

解決方案是Auris和Pointy解釋的。首先,獲取href屬性可以在處理控制檯中解決錯誤。正如Pointy所說,file_exists的意思是「如果文件不存在」。一切正常工作,感謝支持!