它涉及我在网站开发方面的最后一年项目。我想改变我的网页的内容,而不重新加载页面 - 我知道这可以实现使用css/ajax或jQuery,但这只会通过隐藏和显示或replaceWith()在jQuery中。 我希望网址也可以改变。我想实现类似这样的页面https://www.khanacademy.org/computing/computer-programming - 当用户单击INTRO TO JS:绘图和动画时,您将看到页面更改的URL和内容,但页面不会重新加载。更改网址并更新页面内容,无需重新加载
指南将不胜感激。谢谢
小草案:
**MY header.php**
<script language="javascript" src="jquery-1.4.4.min.js"></script>
<script>
$(function(){
\t $("a[rel='tab']").click(function(e){
\t \t //e.preventDefault();
\t \t /* \t
\t \t if uncomment the above line, html5 nonsupported browers won't change the url but will display the ajax content;
\t \t if commented, html5 nonsupported browers will reload the page to the specified link.
\t \t */
\t \t
\t \t //get the link location that was clicked
\t \t pageurl = $(this).attr('href');
\t \t
\t \t //to get the ajax content and display in div with id 'content'
\t \t $.ajax({url:pageurl+'?rel=tab',success: function(data){
\t \t \t $('#content').html(data);
\t \t }});
\t \t
\t \t //to change the browser URL to 'pageurl'
\t \t if(pageurl!=window.location){
\t \t \t window.history.pushState({path:pageurl},'',pageurl); \t
\t \t }
\t \t return false;
\t });
});
/* the below code is to override back button to get the ajax content without reload*/
$(window).bind('popstate', function() {
\t $.ajax({url:location.pathname+'?rel=tab',success: function(data){
\t \t $('#content').html(data);
\t }});
});
</script>
<div id='menu'>
\t <a rel='tab' href='http://localhost/html5-history-api/menu1.php'>menu1</a> |
\t <a rel='tab' href='http://localhost/html5-history-api/menu2.php'>menu2</a> |
\t <a rel='tab' href='http://localhost/html5-history-api/menu3.php'>menu3</a></div>
我menu1.php(MENU2和3的相同代码)
<?php
if($_GET['rel']!='tab'){
\t include 'header.php';
\t echo "<div id='content'>";
}
?>
menu1 content in menu1.php
<?php
if($_GET['rel']!='tab'){
\t echo "</div>";
\t include 'footer.php';
}?>
我想,当我点击链接 - 链接消失,它如果您使用的是Chrome浏览器显示只有像只“在menu1.php MENU1内容”,并没有其他的页面
? –
检查这个http://stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page –
看看Angular JS,更具体地说,这个概念叫做“单页应用程序(SPA)”。 Gmail,脸谱等等现在都在使用相同的体系结构 – AdityaParab