zsydgithub 2 years ago
parent
commit
2f6107849c
4 changed files with 195 additions and 0 deletions
  1. 59 0
      7_Html5/5_选择器.html
  2. 32 0
      7_Html5/6_属性.html
  3. 36 0
      7_Html5/7_深克隆、浅克隆.html
  4. 68 0
      7_Html5/8_深拷贝.html

+ 59 - 0
7_Html5/5_选择器.html

@@ -0,0 +1,59 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+  <meta charset="UTF-8">
+  <meta http-equiv="X-UA-Compatible" content="IE=edge">
+  <meta name="viewport" content="width=device-width, initial-scale=1.0">
+  <title>Document</title>
+</head>
+<body>
+  <div id="div1">hello</div>
+  <div class="content">word</div>
+  <div class="content"> !!!</div>
+  <ul id="ul1">
+    <li>1</li>
+    <li>2</li>
+    <li>3</li>
+  </ul>
+  <ul id="ul2">
+    <li>4</li>
+    <li>5</li>
+    <li>6</li>
+  </ul>
+  <script>
+    // var div1 = document.getElementById('div1')
+    // console.log(div1)
+    // var content = document.getElementsByClassName('content')
+    // console.log(content)
+    // var div3 = document.getElementsByTagName('div')
+    // console.log(div3)
+    /* 
+    querySelector 获取元素 
+    智能获取符合条件的一个元素
+    */
+    // var div1 = document.querySelector('#div1')
+    // console.log(div1)
+    // var div2 = document.querySelector('.content')
+    // console.log(div2)
+    // var div3 = document.querySelector('div')
+    // console.log(div3)
+    /* querySelectorAll 查找所有满足条件的元素 */
+    /* nodeList 类数组  不是真正的数组  只具有少数数组的方法 */
+    // var aLi = document.querySelectorAll('li')
+    // console.log(aLi)
+    // var a = [1,2,3]
+    // console.log(a)
+
+    // for(var i=0;i<aLi.length;i++){
+    //   aLi[i].onclick = function(){
+    //     console.log(this.innerText)
+    //   }
+    // }
+
+    var ul2 = document.querySelector('#ul2')
+    console.log(ul2)
+    var uli2 = ul2.querySelectorAll('li')
+    console.log(uli2)
+  </script>
+</body>
+</html>

+ 32 - 0
7_Html5/6_属性.html

@@ -0,0 +1,32 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+  <meta charset="UTF-8">
+  <meta http-equiv="X-UA-Compatible" content="IE=edge">
+  <meta name="viewport" content="width=device-width, initial-scale=1.0">
+  <title>Document</title>
+</head>
+<body>
+  <div data-name="cool" data-nameList="123" data-first-name="haha">123</div>
+  <script>
+    var div1 = document.querySelector('div')
+    /* 
+      给元素设定自定义属性
+      第一种不会再html里面看到
+      1.div1.index(属性值)
+      2.div1.setAttribute(属性名,属性值)
+      3.div1.dataset.属性名(属性值)
+
+    */
+    div1.index = 1
+    div1.setAttribute('name','x')
+    div1.dataset.age = 18
+    console.log(div1.index)
+    console.log(div1.getAttribute('name'))
+    console.log(div1.dataset.age)
+    console.log(div1.dataset.name)
+    console.log(div1.dataset.namelist)
+    console.log(div1.dataset.firstName)
+  </script>
+</body>
+</html>

+ 36 - 0
7_Html5/7_深克隆、浅克隆.html

@@ -0,0 +1,36 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+  <meta charset="UTF-8">
+  <meta http-equiv="X-UA-Compatible" content="IE=edge">
+  <meta name="viewport" content="width=device-width, initial-scale=1.0">
+  <title>Document</title>
+</head>
+<body>
+  <script>
+    var person = {
+      name:'zs',
+      age:18
+    }
+    /* 
+      浅拷贝、浅克隆: 将一个变量赋值给另一个变量 修改其中一个变量 原有的变量也随之改变(引用数据类型)
+      深拷贝、深克隆: 将一个变量赋值给另一个变量 修改其中一个变量 原有的变量不跟随改变(基本数据类型)
+    */
+
+    /* 将对象转化为字符串类型 */
+    var str = JSON.stringify(person)
+    // console.log(str)
+    /* 将字符串转化为对象 */
+    var obj = JSON.parse(str)
+    // console.log(obj)
+
+    obj.age = 40
+    console.log(obj)
+    console.log(person)
+    /* 
+    实现深拷贝 可以通过JSON.stringfy()先转化成字符串
+    然后对字符串进行一个复制  在转化为 对象  JSON.parse()
+    */
+  </script>
+</body>
+</html>

+ 68 - 0
7_Html5/8_深拷贝.html

@@ -0,0 +1,68 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+  <meta charset="UTF-8">
+  <meta http-equiv="X-UA-Compatible" content="IE=edge">
+  <meta name="viewport" content="width=device-width, initial-scale=1.0">
+  <title>Document</title>
+</head>
+<body>
+  <script>
+    // var person = {
+    //   name:'zs',
+    //   age: 18
+    // }
+
+    // function deepClone(obj){
+    //   var tmp = {};
+    //   for(key in obj) {
+    //     tmp[key] = obj[key]
+    //   }
+    //   return tmp
+    // }
+    // console.log(deepClone(person))
+
+    // var a = deepClone(person)
+    // a.name = 'lisi'
+    // console.log(a)
+    // console.log(person)
+
+
+      var person = {
+        name:'zs',
+        zge:18,
+        school:{
+          address:'harbin',
+          num: 10000
+        }
+      }
+    // var a = deepClone(person)
+    // a.school.address = 'shenyang'
+    // a.name = 'lisi'
+    // console.log(a)
+    // console.log(person)
+
+    /* 封装深克隆的方法 */
+    function deepClone(obj){
+      /* 定义一个空对象 用于接收深克隆后的结果 */
+      var tmp = {}
+      /* 循环对象下面的每一项 循环对象下面的属性 */
+      for(key in obj){
+        /* 判断当前对象是基本数据类型还是引用数据类型  */
+        if(typeof (obj[key]) == 'object'){
+          /* 如果是引用数据类型 再次调用函数本身 再次实现深克隆 */
+          tmp[key] = deepClone(obj[key])
+        } else {
+          /* 如果要是基本数据类型 直接复制 */
+          tmp[key] = obj[key]
+        }
+      }
+      return tmp
+    }
+    var a = deepClone(person)
+    a.school.address = 'shenyang '
+    console.log(a)
+    console.log(person)
+  </script>
+</body>
+</html>