fengchuanyu 3 달 전
부모
커밋
043e29ad7d
3개의 변경된 파일142개의 추가작업 그리고 0개의 파일을 삭제
  1. 72 0
      9_HTML5/8_hash.html
  2. 21 0
      9_HTML5/9_dataset.html
  3. 49 0
      9_HTML5/练习2_cookie.html

+ 72 - 0
9_HTML5/8_hash.html

@@ -0,0 +1,72 @@
+<!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 id="box">
+        第一个页面
+    </div>
+    <button id="btn1">按钮1</button>
+    <button id="btn2">按钮2</button>
+    <button id="btn3">按钮3</button>
+    <button id="btn4">按钮4</button>
+    <script>
+        // console.log(window.location);
+        let oBtn1 = document.getElementById('btn1');
+        let oBtn2 = document.getElementById('btn2');
+        let oBtn3 = document.getElementById('btn3');
+        let oBtn4 = document.getElementById('btn4');
+        let oBox = document.getElementById('box');
+        let aBtn = document.querySelectorAll('button');
+
+        let obj = {
+            page1: '第一个页面',
+            page2: '第二个页面',
+            page3: '第三个页面',
+            page4: '第四个页面'
+        }
+
+        for(let i = 0;i<aBtn.length;i++){
+            aBtn[i].onclick = function(){
+                let str = obj['page'+(i+1)]
+                console.log(str);
+                oBox.innerHTML = str;
+                location.hash = "page"+(i+1)
+            }
+        }
+
+
+        // oBtn1.onclick = function(){
+        //     oBox.innerHTML = '第一个页面';
+        //     location.hash = "page1"
+        // }
+        // oBtn2.onclick = function(){
+        //     oBox.innerHTML = '第二个页面';
+        //     location.hash = "page2"
+        // }
+        // oBtn3.onclick = function(){
+        //     oBox.innerHTML = '第三个页面';
+        //     location.hash = "page3"
+        // }
+        // oBtn4.onclick = function(){
+        //     oBox.innerHTML = '第四个页面';
+        //     location.hash = "page4"
+        // }
+
+        // 监听hash值的变化
+        window.onhashchange = function(){
+            let str = location.hash.slice(1);
+            console.log(str);
+            for(let key in obj){
+                if(key == str){
+                    oBox.innerHTML = obj[key];
+                }
+            }
+        }
+
+    </script>
+</body>
+</html>

+ 21 - 0
9_HTML5/9_dataset.html

@@ -0,0 +1,21 @@
+<!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 data-val="test1" data-index="1">1</button>
+    <button data-val="test2" data-index="2">2</button>
+    <button data-val="test3" data-index="3">3</button>
+    <script>
+        let btns = document.querySelectorAll('button');
+        for(let i = 0; i < btns.length; i++){
+            btns[i].onclick = function(){
+                console.log(this.dataset)
+            }
+        }
+    </script>
+</body>
+</html>

+ 49 - 0
9_HTML5/练习2_cookie.html

@@ -0,0 +1,49 @@
+<!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>
+    <script>
+        // 1.创建cookie
+        function setCookie(name, value, day) {
+            let timer = new Date();
+            timer.setDate(timer.getDate() + day);
+            document.cookie = `${name}=${value};expires=${timer.toUTCString()}`
+        }
+        // setCookie('school', '黑大', 3);
+        // setCookie('userName', '张三', 3);
+        // setCookie('age', '18', 3);
+
+        // 2.获取cookie
+        function getCookie(cookieName) {
+            let cookieArr = document.cookie.split(';');
+            for (let i = 0; i < cookieArr.length; i++) {
+                let cookieKey = cookieArr[i].split("=")[0].trim();
+                // cookieKey = cookieKey.trim();
+                if (cookieKey === cookieName) {
+                    // console.log(cookieArr[i].split("=")[1]);
+                    return cookieArr[i].split("=")[1];
+                }
+            }
+        }
+        // console.log(getCookie('age'));
+        
+        // 3.删除cookie
+        function removeCookie(cookieName) {
+            // let timer = new Date();
+            // timer.setDate(timer.getDate() - 1);
+            // document.cookie = `${cookieName}=1;expires=${timer.toUTCString()}`
+
+            setCookie(cookieName, 1, -1);
+        }
+
+        removeCookie('userName');
+
+
+
+    </script>
+</body>
+</html>