fengchuanyu 4 månader sedan
förälder
incheckning
5ad4877e65
3 ändrade filer med 123 tillägg och 10 borttagningar
  1. 30 0
      5_DOM/20_下拉列表.html
  2. 15 10
      5_DOM/3_绑定事件.html
  3. 78 0
      5_DOM/练习12_位移练习.html

+ 30 - 0
5_DOM/20_下拉列表.html

@@ -0,0 +1,30 @@
+<!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>
+        select{
+            width: 100px;
+            height: 200px;
+        }
+    </style>
+</head>
+<body>
+    <select id="sel" multiple>
+        <option value="one">one</option>
+        <option value="two">two</option>
+        <option value="three">three</option>
+    </select>
+    <script>
+        var oSel = document.getElementById("sel");
+        console.log(oSel.options);
+        oSel.onclick = function(){
+            console.log(oSel.selectedOptions);
+            console.log(oSel.selectedIndex);
+            console.log(oSel.options[1].selected);
+        }
+    </script>
+</body>
+</html>

+ 15 - 10
5_DOM/3_绑定事件.html

@@ -30,9 +30,9 @@
 
         // }
         // 鼠标移动
-        oBtn.onmousemove = function(){
-            console.log("onmousemove")
-        }
+        // oBtn.onmousemove = function(){
+        //     console.log("onmousemove")
+        // }
 
         // 鼠标移入
         // 事件处理函数中是可以使用this this指向的是触发事件的对象
@@ -57,13 +57,18 @@
         //     console.log("onmouseout");
         // }
 
-        // 鼠标按下
-        oBtn.onmousedown = function(){
-            console.log("onmousedown");
-        }
-        // 鼠标抬起
-        oBtn.onmouseup = function(){
-            console.log("onmouseup");
+        // // 鼠标按下
+        // oBtn.onmousedown = function(){
+        //     console.log("onmousedown");
+        // }
+        // // 鼠标抬起
+        // oBtn.onmouseup = function(){
+        //     console.log("onmouseup");
+        // }
+
+        // 双击事件
+        oBtn.ondblclick = function(){
+            console.log("ondblclick")
         }
 
     </script>

+ 78 - 0
5_DOM/练习12_位移练习.html

@@ -0,0 +1,78 @@
+<!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>
+        /* .box1,.box2{
+            width: 100px;
+            height: 100px;
+            background-color: red;
+        }
+        .box2{
+            background-color:blue;
+        } */
+    </style>
+</head>
+<body>
+    <!-- <div class="box">
+        <div class="box1"></div>
+        <div class="box2"></div>
+    </div> -->
+    <div class="box">
+        <div class="box1">
+            项目一
+            <button class="up-btn">向上</button>
+            <button class="down-btn">向下</button>
+        </div>
+        <div class="box2">
+            项目二
+            <button class="up-btn">向上</button>
+            <button class="down-btn">向下</button>
+        </div>
+        <div class="box3">
+            项目三
+            <button class="up-btn">向上</button>
+            <button class="down-btn">向下</button>
+        </div>
+    </div>
+    <script>
+        // var oBox = document.getElementsByClassName("box")[0];
+        // var oBox1 = document.getElementsByClassName("box1")[0];
+        // var oBox2 = document.getElementsByClassName("box2")[0];
+        // oBox.insertBefore(oBox2,oBox1);
+        var aUpBtn = document.getElementsByClassName("up-btn");
+        var aDownBtn = document.getElementsByClassName("down-btn");
+        var oBox = document.getElementsByClassName("box")[0];
+        for(var i=0;i<aUpBtn.length;i++){
+            // 绑定向上按钮
+            aUpBtn[i].onclick = function(){
+                // 获取当前点击按钮的父元素
+                var oParent = this.parentNode;
+                // 获取当前点击按钮的父元素的上一个兄弟元素
+                var oPre = oParent.previousElementSibling;
+                if(oPre){
+                    // 如果当前点击按钮的父元素的上一个兄弟元素存在 则向前位移
+                    oBox.insertBefore(oParent,oPre);
+                }else{
+                    console.log("已经是第一个了");
+                }
+            }
+            // 绑定向下按钮
+            aDownBtn[i].onclick = function(){
+                //获取当前点击按钮的父元素
+                var oParent = this.parentNode;
+                // 获取当前父元素的后边的兄弟
+                var oNext = oParent.nextElementSibling;
+                if(oNext){
+                    // 如果当前点击按钮的父元素的后边的兄弟存在 则向后位移
+                    oBox.insertBefore(oNext,oParent);
+                }else{
+                    console.log("已经是最后一个了")
+                }
+            }
+        }
+    </script>
+</body>
+</html>