Js的学习第十天
依旧是课堂的内容
运用了排他事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
| <!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> table{ /* border: 1px solid #ccc; */ border-collapse: collapse; text-align: center; } td,th{ border-bottom: 1px solid #ccc; padding: 5px; } button{ border-radius: 20px; box-shadow: none; text-shadow: none;; } .nav{ background-color: #ccc; height: 45px; width: 400px; border: #ccc 1px solid; margin: 100px auto; margin-bottom: 0; } .nav li{ list-style: none; float: left; width: 100px; height: 30px; line-height: 10px; text-align: center; } .nav1{ height: 40px; width: 400px; border: #ccc 1px solid; margin-bottom: 0; } .nav1 li{ list-style: none; float: left; width: 100px; height: 30px; line-height: 10px; text-align: center; } </style> </head> <body> <!-- 主要是对dom的一些排他操作 --> <h1>这是第一节课的内容</h1> <button>按钮1</button> <button>按钮2</button> <button>按钮3</button> <button>按钮4</button> <button>按钮5</button> <br> <button>清空</button> <h1>这是第二节课的内容</h1> <table> <tr style="background-color: rgba(0, 0, 255, 0.603);"> <td>代码</td> <td>名称</td> <td>最新公布净值</td> <td>累计净值</td> <td>前单位净值</td> <td>净值</td> </tr> <tr> <td>001235</td> <td>3个月开发</td> <td>1023</td> <td>10231</td> <td>1022</td> <td>1024</td> </tr> <tr> <td>001235</td> <td>3个月开发</td> <td>1023</td> <td>10231</td> <td>1022</td> <td>1024</td> </tr> <tr> <td>001235</td> <td>3个月开发</td> <td>1023</td> <td>10231</td> <td>1022</td> <td>1024</td> </tr> <tr> <td>001235</td> <td>3个月开发</td> <td>1023</td> <td>10231</td> <td>1022</td> <td>1024</td> </tr> </table> <br> <button>清空</button> <br> <h1>这是第三节课的内容</h1> <div class="nav"> <ul> <li>点击排名</li> <li>观看排行</li> </ul> </div> <br> <div class="nav1"> <ul> <li>今天</li> <li>昨天</li> <li>一周</li> </ul> </div> <div class="cont"> <div> <p>测试1</p> <p>测试2</p> <p>测试3</p> <p>测试4</p> <p>测试5</p> </div> <div> <p>试3</p> <p>测试4</p> <p>试3</p> <p>测试4</p> <p>测试5</p> </div> <div> <p>测试1</p> <p>试2</p> <p>测试3</p> <p>试4</p> <p>测试5</p> </div> <div> <p>试3</p> <p>试4</p> <p>试3</p> <p>试4</p> <p>试5</p> </div> <div> <p>试3</p> <p>试4</p> <p>试3</p> <p>试4</p> <p>试5</p> </div> </div> </body> <script> // 这是第一节课的js代码 var btns = document.getElementsByTagName('button'); for(var i = 0; i < btns.length; i++){ // 给每个按钮添加点击事件 btns[i].onclick = function(){ for(var j = 0; j < btns.length; j++){ // 这是排他事件 btns[j].style.background = 'white'; } this.style.backgroundColor = "pink"; } } // 清空按钮的全部样式 var qingkong = document.getElementsByTagName('button')[5]; qingkong.onclick = function(){ for(var i = 1; i < btns.length; i++){ btns[i].style.background = 'white'; } } // 下面的是第二节课的js代码 var tas = document.getElementsByTagName('tr'); for(var i = 1; i < tas.length; i++){ tas[i].onmouseenter = function(){ for(var j = 1; j < tas.length; j++){ tas[j].style.background = 'white'; } this.style.backgroundColor = 'pink'; } } // 表格的清空样式 var qingkongbiaoge =document.getElementsByTagName('button')[6]; qingkongbiaoge.onclick = function(){ for(var i = 1; i < tas.length; i++){ tas[i].style.background = 'white'; } } // 这是第三节课的内容 var lis = document.querySelectorAll('.nav li'); var divs = document.querySelectorAll('.cont div'); for(var i = 0; i < lis.length; i++){ lis[i].setAttribute("data-index",i) lis[i].onmouseenter = function(){ // lis[i].getAttribute("data-index",i) for(var j = 0; j < lis.length; j++){ lis[j].style.background = '#ccc'; } this.style.backgroundColor = 'red'; var index = this.getAttribute("data-index"); for(i = 0; i<divs.length; i++){ divs[i].style.display = 'none'; } divs[index].style.display = 'block'; } } var lis1 = document.querySelectorAll('.nav1 li'); for(var i = 0; i < lis1.length; i++){ lis1[i].setAttribute("data-index",i) lis1[i].onclick = function(){ for(var j = 0; j < lis1.length; j++){ lis1[j].style.background = ''; } this.style.backgroundColor ='red'; var index = this.getAttribute("data-index"); for(i = 0; i<divs.length; i++){ divs[i].style.display = 'none'; } divs[index].style.display = 'block'; } } </script> </html>
|
下面的是我最终成果作业,有需要自取,但是没有注释
updata:已添加注释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
| <!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> * { padding: 0; margin: 0; } .cont { width: 340px; height: 338px; border-top: solid #333 2.5px; border-left: solid #333 1px; border-bottom: solid #333 1px; border-right: solid #333 1px; margin: 200px auto } .nav { width: auto; height: 42px; border-top: solid white 6px } .nav1 { width: 165px; height: 36px; color: white; background: #333; text-align: center; line-height: 36px; float: left; } .nav2 { height: 22px; position: relative; } .navli { list-style: none; float: left; width: 62px; height: 20px; border: 1px solid #ccc; margin-right: 4px; text-align: center; } .day { padding-top: 6px; list-style: none; display: none; } .day1 { padding-top: 6px; list-style: none; display: none; }
</style> </head> <body> <div class="cont"> <div class="nav"> <div class="nav1">点击排行</div> <div class="nav1">观看排行</div> </div> <div class="nav2"> <ul> <li class="navli">今天</li> <li class="navli">昨天</li> <li class="navli">一周</li> </ul> </div> <div> <ul class="day" style="padding-top: 6px;list-style: none;display: block;"> <li>点击今天</li> <li>点击今天</li> <li>点击今天</li> <li>点击今天</li> <li>点击今天</li> <li>点击今天</li> <li>点击今天</li> <li>点击今天</li> </ul> <ul class="day"> <li>点击昨天</li> <li>点击昨天</li> <li>点击昨天</li> <li>点击昨天</li> <li>点击昨天</li> <li>点击昨天</li> <li>点击昨天</li> <li>点击昨天</li> </ul> <ul class="day"> <li>点击一周</li> <li>点击一周</li> <li>点击一周</li> <li>点击一周</li> <li>点击一周</li> <li>点击一周</li> <li>点击一周</li> <li>点击一周</li> </ul> <ul class="day1"> <li>观看今天</li> <li>点击今天</li> <li>点击今天</li> <li>点击今天</li> <li>点击今天</li> <li>点击今天</li> <li>点击今天</li> <li>点击今天</li> </ul> <ul class="day1"> <li>观看昨天</li> <li>点击昨天</li> <li>点击昨天</li> <li>点击昨天</li> <li>点击昨天</li> <li>点击昨天</li> <li>点击昨天</li> <li>点击昨天</li> </ul> <ul class="day1"> <li>观看一周</li> <li>点击一周</li> <li>点击一周</li> <li>点击一周</li> <li>点击一周</li> <li>点击一周</li> <li>点击一周</li> <li>点击一周</li> </ul> </div> </div> </body> <script> var divs = document.querySelectorAll(".nav1") var trs = document.querySelectorAll(".navli") for (var i = 0; i < trs.length; i++) { trs[i].setAttribute("trindex", i) } var lis = document.querySelectorAll(".day") for (var i = 0; i < lis.length; i++) { lis[i].setAttribute("liindex", i) } var lis1 = document.querySelectorAll(".day1") for (var i = 0; i < lis1.length; i++) { lis1[i].setAttribute("liindex", i) } for (var i = 0; i < divs.length; i++) { divs[i].setAttribute("div-index", i) divs[i].onmouseover = function () { for (var j = 0; j < divs.length; j++) { divs[j].style.background = '#333' } this.style.background = '#ccc' var divindex = this.getAttribute("div-index") console.log(divindex) if (divindex == 0) { for (var i = 0; i < trs.length; i++) { trs[i].onclick = function () { for (var j = 0; j < trs.length; j++) { trs[j].className = 'navli' } this.className = 'navli' var trindex = this.getAttribute("trindex") console.log(trindex) for (var j = 0; j < lis.length; j++) { lis[j].style.display = 'none' lis1[j].style.display = 'none' } lis[trindex].style.display = 'block' } } } if (divindex == 1) { for (var i = 0; i < trs.length; i++) { trs[i].onclick = function () { for (var j = 0; j < trs.length; j++) { trs[j].className = 'navli' } this.className = 'navli' var trindex = this.getAttribute("trindex") console.log(trindex) for (var j = 0; j < lis1.length; j++) { lis1[j].style.display = 'none' lis[j].style.display = 'none' } lis1[trindex].style.display = 'block' console.log(lis1[trindex]) } } } } } </script> </html>
|