这是关于我的网站开发的毕业项目。我想在不重新加载页面的情况下更改页面内容- 我知道可以使用CSS / AJAX或jQuery实现这一点,但这只能通过jQuery中的hide和show或replaceWith()来实现。
我还想更改URL。我想实现类似于此页面https://www.khanacademy.org/computing/computer-programming - 当用户点击“INTRO TO JS:Drawing and animation”时,您将看到页面的URL和内容发生变化,但页面不会重新加载。
希望能得到指导。谢谢。
我的menu1.php文件(menu2和menu3的代码相同)。
我想要的是,当我点击链接时,链接会消失,只显示内容,例如“在menu1.php中的菜单1内容”,页面上没有其他内容。
希望能得到指导。谢谢。
**MY header.php**
<script language="javascript" src="jquery-1.4.4.min.js"></script>
<script>
$(function(){
$("a[rel='tab']").click(function(e){
//e.preventDefault();
/*
if uncomment the above line, html5 nonsupported browers won't change the url but will display the ajax content;
if commented, html5 nonsupported browers will reload the page to the specified link.
*/
//get the link location that was clicked
pageurl = $(this).attr('href');
//to get the ajax content and display in div with id 'content'
$.ajax({url:pageurl+'?rel=tab',success: function(data){
$('#content').html(data);
}});
//to change the browser URL to 'pageurl'
if(pageurl!=window.location){
window.history.pushState({path:pageurl},'',pageurl);
}
return false;
});
});
/* the below code is to override back button to get the ajax content without reload*/
$(window).bind('popstate', function() {
$.ajax({url:location.pathname+'?rel=tab',success: function(data){
$('#content').html(data);
}});
});
</script>
<div id='menu'>
<a rel='tab' href='http://localhost/html5-history-api/menu1.php'>menu1</a> |
<a rel='tab' href='http://localhost/html5-history-api/menu2.php'>menu2</a> |
<a rel='tab' href='http://localhost/html5-history-api/menu3.php'>menu3</a></div>
<?php
if($_GET['rel']!='tab'){
include 'header.php';
echo "<div id='content'>";
}
?>
menu1 content in menu1.php
<?php
if($_GET['rel']!='tab'){
echo "</div>";
include 'footer.php';
}?>