http://www.queness.com/post/328/a-simple-ajax-driven-website-with-jqueryphp
我试图让上面的脚本工作,但有一些变化。我需要将内容作为 php 包含加载,而不是作为 html 字符串加载。我现在的主要问题是我认为代码没有指向正确的页面,因此没有新内容。
php:
switch($_GET['page']) {
case 'code' : $page = include ($_SERVER['DOCUMENT_ROOT'].'mysite/code.php'); break;
case 'design' : $page = include ($_SERVER['DOCUMENT_ROOT'].'mysite/design.php'); break;
case 'writing' : $page = include ($_SERVER['DOCUMENT_ROOT'].'mysite/writing.php'); break;
html:
<ul id="menu">
<li><a title="index" href="#index.php" rel="ajax"><span>home</span></a></li>
<li><a title="code" href="#code.php" rel="ajax"><span>code</span></a></li>
<li><a title="design" href="#design.php" rel="ajax"><span>design</span></a></li>
<li><a title="illustration" href="#illustration.php" rel="ajax"><span>illustration</span></a></li>
<li><a title="writing" href="#writing.php" rel="ajax"><span>writing</span></a></li>
<li><a title="links" href="#links.php" rel="ajax"><span>links</span></a></li>
<li><a title="about" href="#about.php" rel="ajax"><span>about</span></a></li>
</ul>
javascript:
<script type="text/javascript">
$(document).ready(function () {
//Check if url hash value exists (for bookmark)
$.history.init(pageload);
//highlight the selected link
$('a[href=' + document.location.hash + ']').addClass('selected');
//Seearch for link with REL set to ajax
$('a[rel=ajax]').click(function () {
//grab the full url
var hash = this.href;
//remove the # value
hash = hash.replace(/^.*#/, '');
//for back button
$.history.load(hash);
//clear the selected class and add the class class to the selected link
$('a[rel=ajax]').removeClass('selected');
$(this).addClass('selected');
//hide the content and show the progress bar
$('#content').hide();
$('#loading').show();
//run the ajax
getPage();
//cancel the anchor tag behaviour
return false;
});
});
function pageload(hash) {
//if hash value exists, run the ajax
if (hash) getPage();
}
function getPage() {
//generate the parameter for the php script
var data = 'page=' + document.location.hash.replace(/^.*#/, '');
$.ajax({
url: "loader.php",
type: "GET",
data: data,
cache: false,
success: function (html) {
//hide the progress bar
$('#loading').hide();
//add the content retrieved from ajax and put it in the #content div
$('#content').html(html);
//display the body with fadeIn transition
$('#content').fadeIn('slow');
}
});
}
</script>