【发布时间】:2012-09-14 07:09:51
【问题描述】:
以下代码继续将新 URL 推送到状态对象,同时动态更改页面内容。但是,当我开始按下Back 按钮并返回原始页面时,原始内容不显示,而是保留了下一页的内容。如何实现?
<!DOCTYPE html>
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
a = 0;
$("p").click(function(){
var stateObj = { note : ++a };
history.pushState(stateObj, "page 2", "http://localhost/foo/"+a);
$(this).text(a);
});
window.addEventListener('popstate', function(e){
if (history.state){
$("p").text(e.state.note);
if (location.href == 'http://localhost/hist.php') { $('p').text('If you click on me, I will disappear.'); }
}
}, false);
$("div").click(function() { alert(e.state.note); });
});
</script>
</head>
<body>
<p>If you click on me, I will disappear.</p>
<div>Hi</div>
</body>
</html>
【问题讨论】:
标签: javascript jquery html html5-history