我想从另一个页面使用ajax加载div,但使用历史API维护URL完整性。 ajax和历史部分正在工作(即div加载和url正在改变),但由于某种原因,我的div包含我的整个页面,而不仅仅是在div中意味着什么!为什么我的整个网页都填充了我的div?
原始页面和正在加载的页面div都是这种情况。
剥去HTML
<!DOCTYPE html>
<html>
<body>
<div id="slider">
... all the page content
<a rel='tab' href="p/password.jsf">Forgot password?</a>
</div>
</body>
</html>
JS
<script>
$(function(){
$("a[rel='tab']").click(function(e){
//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){
$('#slider').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){
$('#slider').html(data);
}});
});
</script>
因为ajax读取页面响应(这是整页)..所以对Ajax的响应是什么在页面中排序所见即所得thingy – 2013-03-11 16:39:10