JS——tab函數封裝
阿新 • • 發佈:2017-11-22
show oat index 數組 img 500px func lock padding
1、為li標簽添加index屬性,這個屬性正好就是span標簽數組的index值
2、函數封裝適合頁面有多個tab切換,需要註意的在獲取的li標簽和span標簽對象時,必須將對應div對象作為參數傳入
li標簽添加index屬性
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> * { padding: 0; margin:0; } .box1 { width: 500px; height: 400px; border: 1px solid #b6ff00; overflow: hidden; margin: 100px auto; } ul { width: 510px; height: 40px; list-style: none; } li {float: left; width: 101px; height: 40px; text-align: center; font: 400 15px/40px "simsun"; background-color: beige; cursor: pointer; } span { display: none; width: 500px; height: 360px; background-color: #ffd800; text-align: center; font: 700 150px/360px "simsun"; } .current { background-color: #ffd800; } .show { display: block; } </style> </head> <body> <div class="box1"> <ul> <li>鞋子</li> <li>上衣</li> <li>下裝</li> <li>棉衣</li> <li>夏裝</li> </ul> <span>鞋子</span> <span>上衣</span> <span>下裝</span> <span>棉衣</span> <span>夏裝</span> </div> <script> var lis = document.getElementsByTagName("li"); var sps = document.getElementsByTagName("span"); for (var i = 0; i < lis.length; i++) { lis[i].index = i; lis[i].onmouseover = function () { for (var j = 0; j < lis.length; j++) { lis[j].className = ""; sps[j].className = ""; } this.className = "current"; sps[this.index].className = "show"; } } </script> </body> </html>
函數封裝版本
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> * { padding: 0; margin: 0; } .box { width: 500px; height: 400px; border: 1px solid #b6ff00; overflow: hidden; margin: 100px auto; } ul { width: 510px; height: 40px; list-style: none; } li { float: left; width: 101px; height: 40px; text-align: center; font: 400 15px/40px "simsun"; background-color: beige; cursor: pointer; } span { display: none; width: 500px; height: 360px; background-color: #ffd800; text-align: center; font: 700 150px/360px "simsun"; } .current { background-color: #ffd800; } .show { display: block; } </style> </head> <body> <div class="box"> <ul> <li>鞋子</li> <li>上衣</li> <li>下裝</li> <li>棉衣</li> <li>夏裝</li> </ul> <span>鞋子</span> <span>上衣</span> <span>下裝</span> <span>棉衣</span> <span>夏裝</span> </div> <div class="box"> <ul> <li>鞋子</li> <li>上衣</li> <li>下裝</li> <li>棉衣</li> <li>夏裝</li> </ul> <span>鞋子</span> <span>上衣</span> <span>下裝</span> <span>棉衣</span> <span>夏裝</span> </div> <div class="box"> <ul> <li>鞋子</li> <li>上衣</li> <li>下裝</li> <li>棉衣</li> <li>夏裝</li> </ul> <span>鞋子</span> <span>上衣</span> <span>下裝</span> <span>棉衣</span> <span>夏裝</span> </div> <script> var boxs = document.getElementsByClassName("box"); for (var i = 0; i < boxs.length; i++) { tabSwitch(boxs[i]); } function tabSwitch(ele) { var lis = ele.getElementsByTagName("li"); var sps = ele.getElementsByTagName("span"); for (var i = 0; i < lis.length; i++) { lis[i].index = i; lis[i].onmouseover = function () { for (var j = 0; j < lis.length; j++) { lis[j].className = ""; sps[j].className = ""; } this.className = "current"; sps[this.index].className = "show"; } } } </script> </body> </html>
JS——tab函數封裝