fengchuanyu 3 ماه پیش
والد
کامیت
f4e74a14d8
2فایلهای تغییر یافته به همراه105 افزوده شده و 0 حذف شده
  1. 36 0
      9_HTML5/10_history.html
  2. 69 0
      9_HTML5/练习题3_历史管理hash.html

+ 36 - 0
9_HTML5/10_history.html

@@ -0,0 +1,36 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+</head>
+<body>
+    <div class="box">
+        第一个页面
+    </div>
+    <button data-index="0">按钮1</button>
+    <button data-index="1">按钮2</button>
+    <button data-index="2">按钮3</button>
+    <script>
+        let btns = document.querySelectorAll('button');
+        let oBox = document.querySelector('.box');
+        let textArr = ["第一个页面", "第二个页面", "第三个页面"];
+        
+        btns.forEach(function(item){
+            item.onclick = function(){
+                let index = this.dataset.index;
+                oBox.innerHTML = textArr[index];
+                window.history.pushState(index,"");
+            }
+        });
+
+        window.onpopstate = function(){
+            let index = window.history.state*1;
+            console.log(index);
+            oBox.innerHTML = textArr[index];
+        }
+
+    </script>
+</body>
+</html>

+ 69 - 0
9_HTML5/练习题3_历史管理hash.html

@@ -0,0 +1,69 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Document</title>
+    <style>
+        .box div{
+            width: 200px;
+            height: 120px;
+            background-color: #aaa;
+            color: #fff;
+            font-size: 40px;
+            font-weight: bolder;
+            text-align: center;
+            line-height: 120px;
+        }
+        .box{
+            display: flex;
+        }
+        .box .active{
+            background-color: #111;
+        }
+    </style>
+</head>
+<body>
+    <div class="box">
+        <div class="tab-btn active">first</div>
+        <div class="tab-btn">second</div>
+        <div class="tab-btn">third</div>
+    </div>
+    <script>
+        let tabBtns = document.querySelectorAll('.tab-btn');
+        let hashArr = ["first","second","third"];
+        tabBtns.forEach((item,index)=>{
+            item.onclick = function(){
+                // 管理选中状态
+                // tabBtns.forEach((item)=>{
+                //     item.classList.remove("active");
+                // });
+                // this.classList.add("active");
+                // 管理hash
+                location.hash = hashArr[index]
+            }
+        })
+
+        // 监听hash变化
+        window.onhashchange = function(){
+            // 获取hash值
+            let hashVal = location.hash.slice(1);
+            // 根据hash值,管理选中状态
+            // 获取hash值对应的索引 
+            // let index = hashArr.indexOf(hashVal);
+            let index = hashArr.findIndex((item)=>{
+                // if(item == hashVal){
+                //     return true;
+                // }
+                return item == hashVal;
+            })
+            tabBtns.forEach((item)=>{
+                item.classList.remove("active");
+            });
+            tabBtns[index].classList.add("active");
+        }
+
+
+    </script>
+</body>
+</html>