fengchuanyu 1 hete
szülő
commit
af81f2a183

+ 52 - 0
6-HTML5/10_历史管理history.html

@@ -0,0 +1,52 @@
+<!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>
+    <h1>内容一</h1>
+    <button id="btn1">按钮一</button>
+    <button id="btn2">按钮二</button>
+    <button id="btn3">按钮三</button>
+    <script>
+        var oBtn1 = document.querySelector("#btn1");
+        var oBtn2 = document.querySelector("#btn2");
+        var oBtn3 = document.querySelector("#btn3");
+        var oH1 = document.querySelector("h1");
+
+        oBtn1.onclick = function () {
+            // 向历史记录中添加一条记录 history.pushState
+            // 一共有两个参数 第一个参数是状态值 第二个参数是标题(一般不用传输但必有这个参数所以传空值)
+            history.pushState(1,"");
+            oH1.innerText = "内容一";
+        }
+        oBtn2.onclick = function () {
+            history.pushState(2,"");
+            oH1.innerText = "内容二";
+        }
+        oBtn3.onclick = function () {
+            history.pushState(3,"");
+            oH1.innerText = "内容三";
+        }
+
+        // 监听history值的变化
+        window.onpopstate = function(){
+            console.log(history.state);
+            if(history.state == 1){
+                oH1.innerText = "内容一";
+            }else if(history.state == 2){
+                oH1.innerText = "内容二";
+            }else if(history.state == 3){
+                oH1.innerText = "内容三";
+            }
+        }
+
+
+    </script>
+</body>
+
+</html>

+ 25 - 0
6-HTML5/11_dataset.html

@@ -0,0 +1,25 @@
+<!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>
+    <!-- 自定义属性 -->
+    <!-- 自定义属性的格式:data-属性名="属性值"  属性名自己起-->
+    <div data-id="1">a</div>
+    <div data-id="2">b</div>
+    <div data-id="3">c</div>
+    <script>
+        var aDiv = document.querySelectorAll("div");
+
+        for(var i=0;i<aDiv.length;i++){
+            aDiv[i].onclick = function(){
+                console.log(this.dataset.id);
+            }
+        }
+
+    </script>
+</body>
+</html>

+ 52 - 0
6-HTML5/9_历史管理hash.html

@@ -0,0 +1,52 @@
+<!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>
+    <h1>内容一</h1>
+    <button id="btn1">按钮一</button>
+    <button id="btn2">按钮二</button>
+    <button id="btn3">按钮三</button>
+    <script>
+        var oBtn1 = document.querySelector("#btn1");
+        var oBtn2 = document.querySelector("#btn2");
+        var oBtn3 = document.querySelector("#btn3");
+        var oH1 = document.querySelector("h1");
+
+        oBtn1.onclick = function(){
+            // 设置 hash 值以改变 浏览器地址栏 历史
+            // location.hash = '#hello';
+            location.hash = "#1"
+            oH1.innerText = "内容一";
+        }
+        oBtn2.onclick = function(){
+            location.hash = "#2";
+            oH1.innerText = "内容二";
+        }
+        oBtn3.onclick = function(){
+            location.hash = "#3";
+            oH1.innerText = "内容三";
+        }
+
+
+
+        // 监听 hash 值的改变
+        window.onhashchange = function(){
+            // console.log("132")
+            // 当点击向前 或者 向后按钮 时 会触发 hashchange 事件
+            // 获取当前 hash 值
+            var thisHash = location.hash;
+            if(thisHash == "#1"){
+                oH1.innerText = "内容一";
+            }else if(thisHash == "#2"){
+                oH1.innerText = "内容二";
+            }else if(thisHash == "#3"){
+                oH1.innerText = "内容三";
+            }
+        }
+    </script>
+</body>
+</html>