fengchuanyu 1 day ago
parent
commit
da60a75566

+ 31 - 0
4_BOM&DOM/10_鼠标移入事件.html

@@ -0,0 +1,31 @@
+<!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: 200px;
+            background-color: blue;
+        }
+    </style>
+</head>
+<body>
+    <div id="box"></div>
+    <script>
+        // 获取元素
+        var box = document.getElementById("box");
+        // 绑定事件
+        // 鼠标移入事件 mouseover
+        // 鼠标移出事件 mouseout
+        box.onmouseover = function(){
+            console.log("鼠标移入了");
+        }
+        box.onmouseout = function(){
+            console.log("鼠标移出了");
+        }
+    </script>
+</body>
+</html>

+ 36 - 0
4_BOM&DOM/9_控制属性.html

@@ -0,0 +1,36 @@
+<!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>
+    <img class="img" src="./img/phone.png" alt="phone">
+    <button id="btn">切换图片</button>
+    <script>
+        // 获取元素
+        var img = document.getElementsByTagName("img")[0];
+        var btn = document.getElementById("btn");
+
+        // 绑定事件
+        btn.onclick = function(){
+            // 方法一 通过setAttribute()方法设置属性值
+            // 通过属性去控制标签
+            // getAttribute()方法可以获取元素的属性值 一个参数 括号内部为属性名
+            // setAttribute()方法可以设置元素的属性值 两个参数  第一个为属性名,第二个为属性值
+            // var imgSrc = img.getAttribute("src");
+            // var imgAlt = img.getAttribute("alt");
+            // var imgClass = img.getAttribute("class");
+            // console.log(imgSrc, imgAlt, imgClass);
+            // 切换图片
+            // img.setAttribute("src","./img/phone2.png");
+
+            // 方法二 直接通过属性名赋值
+            // var imgSrc = img.src;
+            // img.src = "./img/phone2.png";
+            // console.log(imgSrc);
+        }
+    </script>
+</body>
+</html>

BIN
4_BOM&DOM/img/phone.png


BIN
4_BOM&DOM/img/phone2.png