fengchuanyu 6 天之前
父節點
當前提交
f4bd04f0fc
共有 3 個文件被更改,包括 176 次插入0 次删除
  1. 32 0
      6_HTML5/9_dataset.html
  2. 83 0
      6_HTML5/练习题3_hash历史管理.html
  3. 61 0
      6_HTML5/练习题4_history历史管理.html

+ 32 - 0
6_HTML5/9_dataset.html

@@ -0,0 +1,32 @@
+<!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>
+    <!-- dataset属性 格式: data-自定义的属性名 -->
+    <div id="box">
+        <ul>
+            <li data-index="0" data-id="1001">a</li>
+            <li data-index="1" data-id="1002">b</li>
+            <li data-index="2" data-id="1003">c</li>
+            <li data-index="3" data-id="1004">d</li>
+            <li data-index="4" data-id="1005">e</li>
+        </ul>
+    </div>
+    <script>
+        // 获取元素
+        var lis = document.querySelectorAll("li");
+
+        for(var i=0;i<lis.length;i++){
+            lis[i].onclick = function(){
+                var index = this.dataset.index;
+
+                console.log(this.dataset.id);
+            }
+        }
+    </script>
+</body>
+</html>

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

@@ -0,0 +1,83 @@
+<!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;
+            background-color: #999;
+            margin:100px auto;
+        }
+        .box ul{
+            display: flex;
+        }
+        .box li{
+            width: 200px;
+            height: 100px;
+            font-size: 50px;
+            font-weight: bold;
+            text-align: center;
+            line-height: 100px;
+            color: white;
+        }
+        .box ul .active{
+            background-color: #111;
+
+        }
+    </style>
+</head>
+<body>
+    <div class="box">
+        <ul>
+            <li class="active">first</li>
+            <li>second</li>
+            <li>thrid</li>
+        </ul>
+    </div>
+    <script>
+        var lis = document.querySelectorAll("li");
+
+        // 循环绑定事件
+        for(var i=0;i<lis.length;i++){
+            lis[i].onclick = function(){
+                // 点击li时,移除所有active类名
+                for(var j=0;j<lis.length;j++){
+                    lis[j].classList.remove("active");
+                }
+                // 点击li时,为当前li添加active类名
+                this.classList.add("active");
+                // 使用hash模式添加历史记录
+                location.hash = this.innerText;
+            }
+        }
+        // 监听hashchange事件
+        window.onhashchange = function(){
+            // 获取当前hash值
+            var hash = location.hash;
+            // 截取字符串#后面的内容
+            hash = hash.substring(1);
+            console.log(hash);
+            // 根据hash值,切换active类名
+            for(var i=0;i<lis.length;i++){
+                if(lis[i].innerText == hash){
+                    lis[i].classList.add("active");
+                }else{
+                    lis[i].classList.remove("active");
+                }
+            }
+
+        }
+
+    </script>
+</body>
+</html>

+ 61 - 0
6_HTML5/练习题4_history历史管理.html

@@ -0,0 +1,61 @@
+<!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;
+            background-color: #999;
+            margin:100px auto;
+        }
+        .box ul{
+            display: flex;
+        }
+        .box li{
+            width: 200px;
+            height: 100px;
+            font-size: 50px;
+            font-weight: bold;
+            text-align: center;
+            line-height: 100px;
+            color: white;
+        }
+        .box ul .active{
+            background-color: #111;
+
+        }
+    </style>
+</head>
+<body>
+    <div class="box">
+        <ul>
+            <li class="active">first</li>
+            <li>second</li>
+            <li>thrid</li>
+        </ul>
+    </div>
+    <script>
+        var lis = document.querySelectorAll("li");
+
+        // 循环绑定事件
+        for(var i=0;i<lis.length;i++){
+            lis[i].onclick = function(){
+                for(var j=0;j<lis.length;j++){
+                    lis[j].classList.remove("active");
+                }
+                this.classList.add("active");
+            }
+        }
+    </script>
+</body>
+</html>