http://www.queness.com/post/328/a-simple-ajax-driven-website-with-jqueryphpPHP的包括內容不加載,可能哈希標籤/地址發行
我試圖得到上面的腳本工作,但也有一些變化。我需要的內容加載爲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>
爲什麼有三聯「的情況下,包括文件時要小心'碼'」?更改爲「案例'設計'」和「案例'寫'」 – pleerock
爲什麼連續三次使用'case'code''? –
也將標題+「代碼」改爲標題=「代碼」 – pleerock