我是使用html 5歷史對象的新手。我嘗試下面的代碼:html 5歷史恢復一些變化,而不是別人?
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
history.pushState({somethinggoeshere:'but what?'}, 'Does a title go here?', '?begin=true');
window.addEventListener('popstate', function(event) {
console.log('popstate fired!');
console.log(event.state);
});
function dosomething()
{
document.getElementById('box').style.display = 'block';
document.getElementById('iframe').src = 't2.html';
history.pushState({somethinggoeshere:'but what?'}, 'Does a title go here?', '?complete=true');
}
</script>
</head>
<body>
<a onclick="dosomething();">Show box</a>
<div id="box" style="background:red; height:100px; display:none;"></div>
<iframe id="iframe" src="t1.html"></iframe>
</body>
</html>
當我按下顯示框,下方會出現一個紅色的框,和iframe的內容從t1.html去t2.html。當我在Chrome瀏覽器中按下後退按鈕時,iframe的內容會返回到t1.html,但紅色框不會消失。
爲什麼後退按鈕恢復IFRAME而不是CSS樣式的紅盒子的內容(回顯示:無);
謝謝Rocket,我在上面的問題描述中加粗了問題。我想這是我困惑的真正問題。 – John
後退和前進按鈕只是更改歷史記錄。歷史只是一個URL列表。它不包含有關該頁面的任何信息。您需要自己重新顯示紅色框。歷史記錄只是重新加載網址。 –
是的,但後退按鈕如何導致iframe.src從t2.html更改爲t1.html?我期待在onpopstate中以編程方式執行mysqlf? – John