fengchuanyu 1 주 전
부모
커밋
f2c0fc5a2e
2개의 변경된 파일177개의 추가작업 그리고 0개의 파일을 삭제
  1. 91 0
      6-HTML5/练习4_历史管理hash.html
  2. 86 0
      6-HTML5/练习5_历史管理history.html

+ 91 - 0
6-HTML5/练习4_历史管理hash.html

@@ -0,0 +1,91 @@
+<!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>
+        * {
+            margin: 0;
+            padding: 0;
+        }
+
+        li {
+            list-style: none;
+        }
+
+        ul {
+            display: flex;
+        }
+
+        li {
+            width: 200px;
+            height: 100px;
+            color: white;
+            background-color: gray;
+            line-height: 100px;
+            text-align: center;
+            font-size: 40px;
+            font-weight: bold;
+        }
+
+        .active {
+            background-color: black;
+        }
+    </style>
+</head>
+
+<body>
+    <!-- 第一步实现样式 -->
+    <div>
+        <ul>
+            <li class="active">first</li>
+            <li>second</li>
+            <li>third</li>
+        </ul>
+    </div>
+    <script>
+        // 第二步绑定事件
+        // 获取元素
+        var aLi = document.querySelectorAll("li");
+
+        // 循环绑定事件
+        for (var i = 0; i < aLi.length; i++) {
+            aLi[i].onclick = function () {
+                // 移除所有选中状态
+                for (var j = 0; j < aLi.length; j++) {
+                    aLi[j].classList.remove("active");
+                }
+                // 添加类名
+                this.classList.add("active");
+                // 修改hash值增加历史记录
+                location.hash = this.innerText;
+            }
+        }
+
+        // 第三步监听hash值变化
+        window.onhashchange = function () {
+            // 获取hash值
+            var hash = location.hash;
+            // 去掉#
+            hash = hash.substr(1);
+
+            // 移除所有选中状态
+            for (var j = 0; j < aLi.length; j++) {
+                aLi[j].classList.remove("active");
+            }
+
+            // 循环查找对应的hash值
+            for (var i = 0; i < aLi.length; i++) {
+                if (hash == aLi[i].innerText) {
+                    // 添加类名
+                    aLi[i].classList.add("active");
+                }
+            }
+
+        }
+    </script>
+</body>
+
+</html>

+ 86 - 0
6-HTML5/练习5_历史管理history.html

@@ -0,0 +1,86 @@
+<!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>
+        * {
+            margin: 0;
+            padding: 0;
+        }
+
+        li {
+            list-style: none;
+        }
+
+        ul {
+            display: flex;
+        }
+
+        li {
+            width: 200px;
+            height: 100px;
+            color: white;
+            background-color: gray;
+            line-height: 100px;
+            text-align: center;
+            font-size: 40px;
+            font-weight: bold;
+        }
+
+        .active {
+            background-color: black;
+        }
+    </style>
+</head>
+
+<body>
+    <!-- 第一步实现样式 -->
+    <div>
+        <ul>
+            <!-- 添加自定义属性 data-index 来存储索引 -->
+            <li data-index="0" class="active">first</li>
+            <li data-index="1">second</li>
+            <li data-index="2">third</li>
+        </ul>
+    </div>
+    <script>
+        // 第二步绑定事件
+        var aLi = document.querySelectorAll("li");
+
+        // 循环绑定事件
+        for(var i=0;i<aLi.length;i++){
+            aLi[i].onclick = function(){
+                // 移除所有选中状态
+                for(var j=0;j<aLi.length;j++){
+                    aLi[j].classList.remove("active");
+                }
+                // 为自己 (当前点击的按钮)添加状态
+                this.classList.add("active");
+
+                //添加历史记录
+                // 获取自定义属性 data-index的值
+                var dataIndex = this.dataset.index;
+                // 把索引存储到历史记录中
+                history.pushState(dataIndex,"");
+            }
+        }
+        // 第三步监听历史变化
+        window.onpopstate = function(){
+            var historyState = history.state;
+            // 将获取的state值转换为数字
+            historyState = parseInt(historyState);
+
+            // 移除所有选中状态
+            for(var j=0;j<aLi.length;j++){
+                aLi[j].classList.remove("active");
+            }
+
+            // 控制对应li添加选中状态
+            aLi[historyState].classList.add("active");
+
+        }
+    </script>
+</body>
+</html>