我有一个通过ajax动态加载内容的导航。但是,如果我刷新页面或访问其他网址并返回当前内容,我会在第一个菜单标签下看到旧内容。
现在我必须解决这个问题。
index.php包含元素header_registrated.inc.php,navigation.inc.php和main_container.inc.php
的index.php:
<?php include("inc/incfiles/header_registrated.inc.php"); ?>
<?php
if (!isset($_SESSION["userLogin"])) {
echo "<meta http-equiv=\"refresh\" content=\"0; url=http://localhost/project\">";
}
else {
echo "";
}
?>
<?php include("inc/incfiles/navigation.inc.php"); ?>
<?php include("inc/incfiles/main_container.inc.php"); ?>
<?php include("inc/incfiles/footer.inc.php"); ?>
header_registrated.inc.php:
<?php
include ("inc/scripts/mysql_connect.inc.php");
session_start();
$user = $_SESSION["userLogin"];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>title</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script language="JavaScript" src="js/framework/jquery.js"></script>
<script language="JavaScript" src="js/dropdown/window.js"></script>
<script language="JavaScript" src="js/navigation/navigation.js"></script>
</head>
<body>
</body>
navigation.inc.php:
<div class="navigation">
<ul>
<li id="1">
<div id="menuImage1" class="menuImage"></div>
<div class="menuText"><p>Punkt 1</p></div>
<div class="navigationDart"></div>
</li>
<li id="2">
<div id="menuImage2" class="menuImage"></div>
<div class="menuText"><p>Punkt 2</p></div>
</li>
<li id="3">
<div id="menuImage3" class="menuImage"></div>
<div class="menuText"><p>Punkt 3</p></div>
</li>
<li id="4">
<div id="menuImage4" class="menuImage"></div>
<div class="menuText"><p>Punkt 4</p></div>
</li>
<li id="5">
<div id="menuImage5" class="menuImage"></div>
<div class="menuText"><p>Punkt 5</p></div>
</li>
<li id="6">
<div id="menuImage6" class="menuImage"></div>
<div class="menuText"><p>Punkt 6</p></div>
</li>
</ul>
</div>
main_container.inc.php:
<div class="mainContainer">
<div class="containerHeader">
<div class="contentHeader">
</div>
</div>
<div class="contentContainer">
<div class="content">
</div>
<div class="advertisement">
</div>
</div>
</div>
现在通过ajax填充divs内容,cnotentHeader和广告(在文件main_content.inc.php中)。导航也有一些jquery效果,页面刷新后也必须相同。
navigation.js:
$(document).ready(function() {
$.get('inc/incfiles/content_container/header/1.php', function(data) {
$('.contentHeader').html(data);
});
$.get('inc/incfiles/content_container/content/1.php', function(data) {
$('.content').html(data);
});
$.get('inc/incfiles/content_container/advertisement/1.php', function(data) {
$('.advertisement').html(data);
});
var current = '1.php';
$(".navigation li").click(function() {
var quelle = $(this).attr('id') + ".php";
// the current content doesn't load again
if(current === quelle) {
return;
}
current = quelle;
// content
$(".content").fadeOut(function() {
$(this).load("inc/incfiles/content_container/content/" + quelle).fadeIn('normal');
})
// advertisement
$(".advertisement").fadeOut(function() {
$(this).load("inc/incfiles/content_container/advertisement/" + quelle).fadeIn('normal');
})
// header
$(".contentHeader").fadeOut(function() {
$(this).load("inc/incfiles/content_container/header/" + quelle).fadeIn('normal');
})
});
$(".navigation li").click(function() {
$(".menuImage").removeClass("menuImageActive1");
$(".menuImage").removeClass("menuImageActive2");
$(".menuImage").removeClass("menuImageActive3");
$(".menuImage").removeClass("menuImageActive4");
$(".menuImage").removeClass("menuImageActive5");
$(".menuImage").removeClass("menuImageActive6");
});
$("#1").mousedown(function() {
$("#menuImage1").addClass("menuImageClick1"); // new class on mouse button press
});
$("#1").mouseup(function() {
$("#menuImage1").removeClass("menuImageClick1"); //remove class after mouse button release
});
$("#1").click(function() {
$("#menuImage1").addClass("menuImageActive1");
});
$("#2").mousedown(function() {
$("#menuImage2").addClass("menuImageClick2"); // new class on mouse button press
});
$("#2").mouseup(function() {
$("#menuImage2").removeClass("menuImageClick2"); //remove class after mouse button release
});
$("#2").click(function() {
$("#menuImage2").addClass("menuImageActive2");
});
$("#3").mousedown(function() {
$("#menuImage3").addClass("menuImageClick3"); // new class on mouse button press
});
$("#3").mouseup(function() {
$("#menuImage3").removeClass("menuImageClick3"); //remove class after mouse button release
});
$("#3").click(function() {
$("#menuImage3").addClass("menuImageActive3");
});
$("#4").mousedown(function() {
$("#menuImage4").addClass("menuImageClick4"); // new class on mouse button press
});
$("#4").mouseup(function() {
$("#menuImage4").removeClass("menuImageClick4"); //remove class after mouse button release
});
$("#4").click(function() {
$("#menuImage4").addClass("menuImageActive4");
});
$("#5").mousedown(function() {
$("#menuImage5").addClass("menuImageClick5"); // new class on mouse button press
});
$("#5").mouseup(function() {
$("#menuImage5").removeClass("menuImageClick5"); //remove class after mouse button release
});
$("#5").click(function() {
$("#menuImage5").addClass("menuImageActive5");
});
$("#6").mousedown(function() {
$("#menuImage6").addClass("menuImageClick6"); // new class on mouse button press
});
$("#6").mouseup(function() {
$("#menuImage6").removeClass("menuImageClick6"); //remove class after mouse button release
});
$("#6").click(function() {
$("#menuImage6").addClass("menuImageActive6");
});
$("#1").click(function(){
$(".navigationDart").animate({
top: "16px"
}, 500 );
});
$("#2").click(function(){
$(".navigationDart").animate({
top: "88px"
}, 500 );
});
$("#3").click(function(){
$(".navigationDart").animate({
top: "160px"
}, 500 );
});
$("#4").click(function(){
$(".navigationDart").animate({
top: "232px"
}, 500 );
});
$("#5").click(function(){
$(".navigationDart").animate({
top: "304px"
}, 500 );
});
$("#6").click(function(){
$(".navigationDart").animate({
top: "376px"
}, 500 );
});
});
我的想法是使用if(isset($ _ SESSION ['ajaxresponse'])),但我不知道怎么做。
请帮帮我。我感觉我已经搜索了整个网络以找到答案。
答案 0 :(得分:1)
我建议使用一个通过AJAX将PHP与jQuery联系起来的库,可以轻松解决您的问题。图书馆是phery http://phery-php-ajax.net 只需对navigation.js文件进行一些优化:
您需要将AJAX集中(或不集中)在一个文件中,以使其更容易。
它可以放在index.php
function load($data){
$r = new PheryResponse;
if (!isset($_SESSION["userLogin"])) {
return $r->redirect('http://localhost/project');
}
$path = 'inc/incfiles/content_container/';
if (isset($data['id'])){
$id = $data['id']. '.php';
} else {
if (!empty($_SESSION['id'])){
$id = $_SESSION['id'];
} else {
$id = '1.php';
}
}
$_SESSION['id'] = $id; // save the current ID and will load next time the person refreshes the page
ob_start();
include $path.'content/'.$id;
$content = ob_get_clean();
ob_start();
include $path.'advertisement/'.$id;
$ads = ob_get_clean();
ob_start();
include $path.'header/'.$id;
$header = ob_get_clean();
$r
->jquery('.advertisement')->html($ads)
->jquery('.contentHeader')->html($header)
->jquery('.content')->html($content)
;
// etc
return $r->this()->find('.menuImage')->addClass('menuImageActive'); // set the current menuImage of the LI element to menuImageActive
}
session_start();
Phery::instance()->set(array(
'load' => 'load'
))->process();
由于你的JS现在不具备性能,我已经重做了它:
$(function() {
phery.remote('load');
var menu_image = $(".menuImage");
$(document)
.on('click', '.navigation li', function(event){
menu_image.removeClass("menuImageActive");
var top;
switch (event.currentTarget.id){
case '1':
top = "16px";
break;
case '2':
top = "88px";
break;
case '3':
top = "160px";
break;
case '4':
top = "232px";
break;
case '5':
top = "304px";
break;
case '6':
top = "376px";
break;
}
$(event.currentTarget)
.find(".navigationDart").animate({ top: top }, 500);
})
.on('mousedown', '.navigation li', function(event){
$(event.currentTarget).addClass('menuImageClick');
})
.on('mouseup', '.navigation li', function(event){
$(event.currentTarget).removeClass('menuImageClick');
});
});
并且您的navigation.inc.php
必须是:
<div class="navigation">
<ul>
<li id="1" data-remote="load" data-args="{'id':1}">
<div id="menuImage1" class="menuImage"></div>
<div class="menuText"><p>Punkt 1</p></div>
<div class="navigationDart"></div>
</li>
<li id="2" data-remote="load" data-args="{'id':2}">
<div id="menuImage2" class="menuImage"></div>
<div class="menuText"><p>Punkt 2</p></div>
</li>
<li id="3" data-remote="load" data-args="{'id':3}">
<div id="menuImage3" class="menuImage"></div>
<div class="menuText"><p>Punkt 3</p></div>
</li>
<li id="4" data-remote="load" data-args="{'id':4}">
<div id="menuImage4" class="menuImage"></div>
<div class="menuText"><p>Punkt 4</p></div>
</li>
<li id="5" data-remote="load" data-args="{'id':5}">
<div id="menuImage5" class="menuImage"></div>
<div class="menuText"><p>Punkt 5</p></div>
</li>
<li id="6" data-remote="load" data-args="{'id':6}">
<div id="menuImage6" class="menuImage"></div>
<div class="menuText"><p>Punkt 6</p></div>
</li>
</ul>
</div>
似乎您的CSS因HTML的简单性而令人费解。并且您不应该使用ID作为ID,ID需要是您应该在这种情况下重用css类,例如
.menuImageActive {
/* common styles */
}
.menuImageClick {
/* common styles */
}
/* apply per ID */
#menuImage1.menuImageActive {
}
#menuImage1.menuImageClick {
}
#menuImage2.menuImageActive {
}
#menuImage2.menuImageClick {
}
#menuImage3.menuImageActive {
}
#menuImage3.menuImageClick {
}
#menuImage4.menuImageActive {
}
#menuImage4.menuImageClick {
}
#menuImage5.menuImageActive {
}
#menuImage5.menuImageClick {
}
#menuImage6.menuImageActive {
}
#menuImage6.menuImageClick {
}
答案 1 :(得分:0)
如果您需要动态加载的标签,请试用jQuery UI tabs,它内置了AJAX支持。
如果您想要支持AJAX事件,请尝试使用jQuery地址插件。
这里有一个关于如何使用jQuery UI tabs with back/forward history的好答案: