我想從另一個頁面使用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