HTML DOM PopStateEvent 物件


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 物件如何工作 −

點選“確認” −

更新於: 30-7 月-2019

99 次瀏覽

開啟您的 職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.