到目前为止,IE10,firefox4以上的版本,Chrome8以上的版本,Safari5,Opera11以上的版本浏览器支持HTML5中的History API。
HTML:
复制代码
<!DOCTYPE HTML>
<html>
<head>
<title> New Document </title>
<style>
ul,li{list-style:none;}
.container{width:px;border:px solid #ccc;overflow:hidden;}
.container ul{float:left;width:px;}
.container li{width:px;height:px;line-height:px;overflow:hidden;}
.container li a{text-decoration:none;}
.container li.current a{color:red;}
.all-content{width:px;float:left;overflow:hidden;}
</style>
<script src=”jquery-…min.js”></script>
<script src=”index.js”></script>
</head>
<body>
<div class=”container”>
<ul class=”list”>
<li>
<a href=”http://localhost/html/index.php”>测试</a>
</li>
<li>
<a href=”http://localhost/html/index.php”>测试</a>
</li>
<li>
<a href=”http://localhost/html/index.php”>测试</a>
</li>
</ul>
<div class=”all-content”>
<ul class=”content”>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</div>
</body>
</html> </p>
<p>
JS:
复制代码
/**
* HTML history and ajax
*/
$(function(){
var ajax,
currentState;
$(‘.container li’).unbind().bind(‘click’,function(e){
e.preventDefault();
var target = e.target,
url = $(target).attr(‘href’);
!$(this).hasClass(‘current’) && $(this).addClass(‘current’).siblings().removeClass(“current”);
if(ajax == undefined) {
currentState = {
url: document.location.href,
title: document.title,
html: $(‘.content’).html()
};
}
ajax = $.ajax({
type:’POST’,
url: url,
dataType:’json’,
success: function(data){
var html = ”;
if(data.length > ) {
for(var i = , ilist = data.length; i < ilist; i++) {
html += ‘<li>’ +data[i].age+ ‘</li>’ +
‘<li>’ +data[i].name+ ‘</li>’ +
‘<li>’ +data[i].sex+ ‘</li>’;
}
$(‘.content’).html(html);
}
var state = {
url: url,
title: document.title,
html: $(‘.content’).html()
};
history.pushState(state,null,url);
}
});









