我继续得到'没有这样的页面!'消息随时我尝试使用按钮来更改内容 这是 index.html :
<!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!';
?>
似乎脚本运行正常,但不知何故它无法进入新页面的目录(即/pages/forum.html; /pages/login.html等)。 如果有人能告诉我哪里犯了错误,我将不胜感激......:)
答案 0 :(得分:1)
您正在尝试获取DOM元素的.hash:
$('ul li a').click(function (e){
checkURL(this.hash);
取而代之的是获取href属性:
checkURL(this.attr("href"));
.hash仅适用于网址对象
另外,首次使用checkURL();在documentReady之后应抛出错误,因为它与方法定义
不匹配答案 1 :(得分:0)
Auris和Pointy都在解释这个问题。首先,获取href属性会在处理控制台中解决错误。另外!像Pointy所说,file_exists意味着“如果文件不存在”。现在一切都很完美,感谢您的支持!