fengchuanyu 18 godzin temu
rodzic
commit
be45423655

+ 68 - 0
4_BOM&DOM/1_认识BOM&DOM.html

@@ -0,0 +1,68 @@
+<!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>
+        .box{
+            width: 200px;
+            height: 2000px;
+            background-color: red;
+        }
+    </style>
+</head>
+<body>
+    <div class="box"></div>
+    <script>
+        // BOM 浏览器对象模型 是浏览器提供的一个对象模型,用于操作浏览器窗口和文档
+        // window对象是BOM的根对象,所有浏览器对象都是window对象的属性或方法
+        // 例如:alert()、prompt()、confirm()等
+
+        // window.prompt("请输入姓名");
+
+        var a = 10;
+        window.console.log(window.a);
+
+        //window下的 location对象 用于操作浏览器窗口的URL 地址栏
+        console.log(window.location.href);
+
+        // window 下的 方法 可以控制滚动条的位置
+        window.scrollTo(0,100);
+
+        // window 下的 定时器
+        // 定时器分为两种:setTimeout()和setInterval()
+        // setTimeout() 用于在指定的时间后执行一次指定的代码 仅可以执行一次
+        // setInterval() 用于在指定的时间间隔内重复执行指定的代码 可以重复执行执行多次
+        
+        // setTimeout() 有两个参数 第一个参数是回调函数(时间到了后要执行的代码) 第二个参数是时间间隔(单位为毫秒 1000毫秒=1秒)
+        // window.setTimeout(function(){
+        //     console.log("hello world");
+        // },1000);
+
+        // setInterval() 有两个参数 第一个参数是回调函数(时间到了后要执行的代码) 第二个参数是时间间隔(单位为毫秒 1000毫秒=1秒)
+        // window.setInterval(function(){
+        //     console.log("hello world");
+        // },1000);
+
+        // 清除定时器
+        // clearTimeout() 用于清除setTimeout()设置的定时器 括号内填写对应定时器的编号/返回值
+        // clearInterval() 用于清除setInterval()设置的定时器 括号内填写对应定时器的编号/返回值
+        // 例如:clearTimeout(timer);
+        // 例如:clearInterval(timer);
+
+        var timer1 = setTimeout(function(){
+            console.log("hello world");
+        },3000)
+        console.log(timer1);
+        clearTimeout(timer1);
+
+        var timer2 = setInterval(function(){
+            console.log("hello world");
+        },1000)
+        console.log(timer2);
+        clearInterval(timer2);
+
+    </script>
+</body>
+</html>

+ 34 - 0
4_BOM&DOM/2_DOM如何控制内容.html

@@ -0,0 +1,34 @@
+<!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>
+    <div></div>
+    <script>
+        // !!*注意 只要通过js控制元素,必须要先获取到元素对象
+        // 控制内容
+        // 1:通过元素对象的innerText属性来控制元素的内容
+        // innerText属性可以控制元素的内容,也可以获取元素的内容
+        // innerText属性只能控制文本内容,不能当作标签操作
+        var h1 = document.getElementsByTagName("h1");
+        // 设置元素的内容
+        // h1[0].innerText = "hello world";
+        // 获取元素的内容
+        var h1InnerText = h1[0].innerText;
+        console.log(h1InnerText);
+        h1[0].innerText = h1InnerText + " world";
+
+
+        // 2、通过元素对象的innerHTML属性来控制元素的内容
+
+        var oDiv = document.getElementsByTagName("div")[0];
+
+        // oDiv.innerText = "<h1>world</h1>";
+        oDiv.innerHTML = "<h1>world</h1>";
+    </script>
+</body>
+</html>

+ 33 - 0
4_BOM&DOM/2_获取元素.html

@@ -0,0 +1,33 @@
+<!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 class="box">hello world</div>
+    <div id="box3">你好</div>
+
+    <script>
+        // 获取元素
+        // 1:通过标签名获取元素 document.getElementsByTagName("标签名");
+        // 注意:返回的是一个类数组对象,不能直接使用数组的方法 可以循环遍历获取到的元素对象
+        var div1 = document.getElementsByTagName("div");
+        // 可以通过索引来获取到具体的元素对象
+        console.log(div1);
+        console.log(div1[0]);
+
+        // 2:通过类名获取元素 document.getElementsByClassName("类名");
+        // 注意:返回的是一个类数组对象
+        var box2 = document.getElementsByClassName("box");
+        console.log(box2);
+        console.log(box2[0]);
+
+        // 3:通过id获取元素 document.getElementById("id"); 
+        // 注意:返回的是一个元素对象
+        var box3 = document.getElementById("box3");
+        console.log(box3);
+    </script>
+</body>
+</html>