fengchuanyu 9 months ago
parent
commit
036e99b40a
2 changed files with 139 additions and 0 deletions
  1. 32 0
      4_DOM&BOM/12_DOM控制样式类.html
  2. 107 0
      4_DOM&BOM/练习5_TAB标签页.html

+ 32 - 0
4_DOM&BOM/12_DOM控制样式类.html

@@ -0,0 +1,32 @@
+<!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>
+        .active{
+            color: red;
+        }
+    </style>
+</head>
+<body>
+    <div class="box test">hello</div>
+    <script>
+        var oBox = document.getElementsByClassName("box")[0];
+
+        oBox.onmouseenter = function(){
+            // this.setAttribute("class","box active");
+            // this.classList.add("active");
+            // console.log(this.classList)
+            // this.classList.remove("test");
+
+            // this.classList.replace("test","active");
+            
+            // 获取类当中的所有值并且以字符串方式返回
+            this.className = "box active";
+            console.log(this.className)
+        }
+    </script>
+</body>
+</html>

+ 107 - 0
4_DOM&BOM/练习5_TAB标签页.html

@@ -0,0 +1,107 @@
+<!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>
+        body,ul{
+            margin: 0;
+            padding: 0;
+        }
+        li{
+            list-style: none;
+        }
+        .clearfix::after{
+            content: "";
+            display: block;
+            clear: both;
+        }
+        .box{
+            width: 600px;
+            height: 300px;
+            border:1px solid black;
+            margin: 100px auto;
+        }
+        .nav-content{
+            height: 50px;
+            background-color: black;
+        }
+        .nav-content li{
+            float: left;
+            color: #fff;
+            padding: 0 20px;
+            height: 50px;
+            line-height: 50px;
+            cursor: pointer;
+        }
+        .nav-content .active{
+            background-color: #aaa;
+            color: black;
+        }
+        .info-content{
+            padding: 20px;
+        }
+        .info-content > div{
+            display: none;
+        }
+        .info-content .active{
+            display: block;
+        }
+    </style>
+</head>
+
+<body>
+    <div class="box">
+        <div class="nav-content">
+            <ul class="clearfix">
+                <li class="active">第一节课</li>
+                <li >第二节课</li>
+                <li>第三节课</li>
+            </ul>
+        </div>
+        <div class="info-content">
+            <div class="active">
+                第一节课内容
+                第一节课内容
+                第一节课内容
+                第一节课内容
+            </div>
+            <div class="">
+                第二节课内容
+                第二节课内容
+                第二节课内容
+                第二节课内容
+            </div>
+            <div class="">
+                第三节课内容
+                第三节课内容
+                第三节课内容
+                第三节课内容
+            </div>
+        </div>
+    </div>
+    <script>
+        var liBtn = document.getElementsByTagName("li");
+        var contentArr = document.getElementsByClassName("info-content")[0].getElementsByTagName("div");
+        console.log(contentArr)
+        // 循环绑定鼠标移入事件
+        for(var i=0;i<liBtn.length;i++){
+            // 创建新属性保持索引值
+            liBtn[i].index = i;
+            liBtn[i].onmouseenter = function(){
+                // 循环清除所有标签按钮的选中状态
+                for(var j=0;j<liBtn.length;j++){
+                    liBtn[j].setAttribute("class","");
+                    contentArr[j].setAttribute("class","");
+                }
+                // 仅给当前按钮加入选中状态
+                this.setAttribute("class","active");
+                contentArr[this.index].setAttribute("class","active");
+            }
+        }
+    </script>
+</body>
+
+</html>