HTML DOM PopStateEvent 对象
htmlweb developmentfront end technology更新于 2025/8/17 11:24:17
HTML DOM popStateEvent 对象是 popstate 事件的事件处理程序,该事件在窗口历史记录发生变化时发生。
PopStateEvent 的属性
| 属性 | 说明 |
|---|---|
| state | 它返回一个表示历史记录条目副本的对象。 |
示例
让我们看一个 HTML DOM popStateEvent 对象的示例 −
<!DOCTYPE html>
<html>
<head>
<style>
html{
height:100%;
}
body{
text-align:center;
color:#fff;
background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) center/cover no-repeat;
height:100%;
}
p{
font-size:1.2rem;
}
.btn{
background:#0197F6;
border:none;
height:2rem;
border-radius:2px;
width:35%;
margin:2rem auto;
display:block;
color:#fff;
outline:none;
cursor:pointer;
}
</style>
</head>
<body>
<h1>DOM PopStateEvent Event Demo</h1>
<button onclick="display()" class="btn">Click Me</button>
<script>
function display(){
window.onpopstate = function(event) {
alert("location: " + document.location +
", state: " + JSON.stringify(event.state));
};
history.pushState({
page: 1
}, "title home", "?page=home");
history.pushState({
page: 2
}, "title about", "?page=about");
history.replaceState({
page: 3
}, "title contact", "?page=contact");
history.back();
history.back();
}
</script>
</body>
</html>
输出
这将产生以下输出 −

点击"点击我"按钮,了解 PopStateEvent 对象的工作原理 −

点击"确定" −

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

