リンクをクリックした後に外部phpファイルを表示し、別のリンクをクリックした後にそのすぐ下に別の外部ファイル(の代わりではない)を表示する方法があるかどうか知りたいです。これが私のコードです。
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery- 1.2.6.pack.js"></script>
<script type="text/javascript" src="core.js"></script>
</head>
<body>
<div id="menu">
<ul>
<li id="home"><a href="#downloads">DOWNLOADS</a></li>
<li id="tutorials"><a href="#errors">ERRORS</a></li>
</ul>
</div>
<div id="content">
</div>
</body>
</html>
core.js
//On load page, init the timer which check if the there are anchor changes each 300 ms
$().ready(function(){
setInterval("checkAnchor()", 100);
});
var currentAnchor = null;
//Function which chek if there are anchor changes, if there are, sends the ajax petition
function checkAnchor(){
//Check if it has changes
if(currentAnchor != document.location.hash){
currentAnchor = document.location.hash;
//if there is not anchor, the loads the default section
if(!currentAnchor)
query = "page=1";
else
{
//Creates the string callback. This converts the url URL/#main&id=2 in URL/?section=main&id=2
var splits = currentAnchor.substring(1).split('&');
//Get the section
var page = splits[0];
delete splits[0];
//Create the params string
var params = splits.join('&');
var query = "page=" + page + params;
}
//Send the petition
$("#loading").show();
$.get("callbacks.php",query, function(data){
$("#content").html(data);
$("#loading").hide();
});
}
}
downloads.php
<b>DOWNLOADS</b>
errors.php
<b>ERRORS</b>
callbacks.php
<?php
//used to simulate more waiting for load the content, remove on yor projects!
sleep(1);
//Captures the petition and load the suitable section
switch($_GET['page']){
case "errors": include 'errors.php'; break;
case "downloads": include 'downloads.php'; break;
default: include 'downloads.php'; break;
}
?>
これは、スイッチを使用し、errors.phpとdownloads.phpの両方を同時に表示できるようにしたいことを除いて、完全に機能します。どちらか一方だけではありません。
編集
明確にするための擬似コード:
ダウンロードをクリックした場合は、download.phpのみを表示します。エラーがクリックされた場合は、error.phpのみを表示し(downloads.phpの直後)、downloads.phpまたはメインページにすでに含まれている場合と含まれていない場合があるその他の外部ファイルを削除しないでください。
助言がありますか?
ps私はこれについて多くのスレッドを調べましたが、それが私が試したすべてのコードを投稿できない理由です(申し訳ありませんが、リンクを含めることもできません。前回、私の質問がそれを行うことに反対しました...> :/)宿題を済ませたことを約束できます。
pssこれが反対票に値すると思われる場合は、その理由を説明してください。私は批判を受け入れますが、親指を下に向けるだけではまったく役に立ちません。
編集:
core.jsをに更新しました
$(document).ready(function(){
$('#menu li a').click(function() {
var currentAnchor = $(this).attr('href');
if(!currentAnchor)
var query = "page=1";
else
{
var splits = currentAnchor.substring(1).split('&');
//Get the section
var page = splits[0];
delete splits[0];
//Create the params string
var params = splits.join('&');
var query = "page=" + page + params;
}
//Send the petition
$("#loading").show();
$.get("callbacks.php",query, function(data){
$("#content").html(data);
$("#loading").hide();
});
return false;
});
});