fengchuanyu 1 mēnesi atpakaļ
vecāks
revīzija
f25c61bb01

+ 29 - 0
6_HTML5/10_dataset.html

@@ -0,0 +1,29 @@
+<!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>
+    <ul>
+        <!-- dataset 是html5新增的属性 可以在元素上添加自定义属性 来保存一些数据 -->
+         <!-- data- 自定义属性 必须以data-开头 后面可以自定义属性名 -->
+        <li data-id="1001" data-age="20">张三</li>
+        <li data-id="1002" data-age="19">李四</li>
+        <li data-id="1003" data-age="18">王五</li>
+    </ul>
+    <script>
+        var aLi = document.querySelectorAll("li");
+        for(var i=0;i<aLi.length;i++){
+            aLi[i].onclick = function(){
+                console.log(this.innerText);
+                // 可以通过dataset属性来获取自定义属性的值
+                console.log(this.dataset.age);
+                console.log(this.dataset.id);
+                
+            }
+        }
+    </script>
+</body>
+</html>

+ 29 - 0
6_HTML5/9_历史管理history.html

@@ -0,0 +1,29 @@
+<!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>
+    <button>添加历史记录</button>
+    <script>
+        var oBtn = document.querySelector("button");
+        var num = 0;
+        oBtn.onclick = function(){
+            // 添加历史记录
+            num++;
+            history.pushState("h1"+num,"");
+        }
+        // 监听history变化
+        window.onpopstate = function(e){
+            console.log("history变化了",e.state);
+            console.log(history.state);
+        }
+
+        // hash 和 history 区别
+        // hash 是url的片段 
+        // history 是浏览器的历史记录 
+    </script>
+</body>
+</html>

+ 88 - 0
6_HTML5/练习3_hash历史管理.html

@@ -0,0 +1,88 @@
+<!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;
+        }
+
+        .box {
+            width: 600px;
+            height: 100px;
+            margin: 100px auto;
+        }
+
+        .box ul {
+            display: flex;
+        }
+
+        .box li {
+            width: 200px;
+            height: 100px;
+            background-color: #aaa;
+            font-size: 50px;
+            text-align: center;
+            line-height: 100px;
+            color: #fff;
+        }
+
+        .box .active {
+            background-color: #111;
+        }
+    </style>
+</head>
+
+<body>
+    <div class="box">
+        <ul>
+            <li class="first active">first</li>
+            <li class="second">second</li>
+            <li class="third">third</li>
+        </ul>
+    </div>
+    <script>
+        var aLi = document.querySelectorAll("li");
+        for (var i = 0; i < aLi.length; i++) {
+            aLi[i].onclick = function () {
+                var innerText = this.innerText;
+                window.location.hash = innerText;
+                for (var j = 0; j < aLi.length; j++) {
+                    aLi[j].classList.remove("active");
+                }
+                this.classList.add("active");
+            }
+        }
+
+        window.onhashchange = function(){
+            var hash = window.location.hash;
+            hash = hash.slice(1);
+            console.log(hash);
+            if(hash == ""){
+                for(var j=0;j<aLi.length;j++){
+                    aLi[j].classList.remove("active");
+                }
+                aLi[0].classList.add("active");
+                return;
+            }
+            for(var i=0;i<aLi.length;i++){
+                if(aLi[i].innerText == hash){
+                    aLi[i].classList.add("active");
+                }else{
+                    aLi[i].classList.remove("active");
+                }
+            }
+
+        }
+    </script>
+</body>
+
+</html>