我想知道是否有一种方法可以在单击链接后显示外部php文件,然后在单击其他链接后在其下方显示另一个外部文件(而不是INSTEAD)。这是我的代码。
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。我已经浏览了很多关于此的线程,这就是为什么我不能发布所有尝试过的代码的原因(对不起,我也不能包含链接,上次我的问题是因为这样做...>: /)所以我可以保证我已经完成了作业。
ps.s.如果您认为这值得拒绝,请善意地解释原因。我很容易受到批评,但只接受拇指根本没有帮助。
编辑:
将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;
});
});
最佳答案
编辑:
[混乱的部分在这里删除]
-
编辑:
core.js(修订)
//On load page, init the timer which check if the there are anchor changes each 300 ms
$(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;
});
});
-
编辑:
这会将数据(来自下载或错误)“附加”到现有内容上。
$.get("callbacks.php",query, function(data){
$("#content").append(data);
$("#loading").hide();
});
希望这可以帮助。