標籤:
前兩種主要實現一個選項卡的切換,第三種使用閉包看書,建構函式實現多個選項卡的切換:
1.第一種實現實現效果為:
實現代碼為:
<!doctype html><!DOCTYPE html><html lang="en"><head> <meta charset="utf-8"/> <title>tab切換</title> <style type="text/css"> * { font: normal 15px "微軟雅黑"; color: #000; } ul { list-style-type: none; padding-left: 5px; margin-bottom: -2px } a { text-decoration: none; } p { display: block; margin: 10px } li { display: inline-block; border: 1px solid #999; border-bottom: 2px solid #a00; background: #fff; text-align: center; width: 60px; height: 30px; margin: 0 1px; line-height: 30px } ul .active { border-top: 2px solid #a00; border-bottom: 2px solid #fff; } #content { margin: 0; border: 1px solid #ccc; border-top: 2px solid #a00; width: 300px } #content div { display: none } #content .active { display: block; } </style></head><body><div> <ul> <li name="a" onclick="tab(this)" class="active"><a href="#">房產</a></li> <li name="a" onclick="tab(this)"><a href="#">家居</a></li> <li name="a" onclick="tab(this)"><a href="#">二手房</a></li> </ul> <div id="content"> <div class="active" name="con"> <p>20jf鋼帶機額戴鴻慈貸款放款l</p> <p>20jf鋼帶機額戴鴻慈貸款放款l</p> <p>20jf鋼帶機額戴鴻慈貸款放款l</p> </div> <div name="con"> <p>nvmllllmlddnlv來的都來l</p> <p>戴綠帽沒vvmdslnv</p> <p>的女凱迪拉克vk</p> </div> <div name="con"> <p>發V</p> <p>叉路口積分卡很多可沒</p> <p>奉公如法</p> </div> </div></div><script type="text/javascript"> var li_arr = document.getElementsByTagName("li"); var div_arr = document.getElementsByName("con"); var len = li_arr.length; function tab(obj) { for (var i = 0; i < len; i++) { if (li_arr[i] == obj) { li_arr[i].setAttribute("class", "active"); div_arr[i].setAttribute("class", "active"); } else { li_arr[i].setAttribute("class", ""); div_arr[i].setAttribute("class", ""); } } }</script></body></html>
2.第二種實現效果為:
主要實現代碼如下:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <style> *{padding: 0; margin: 0; box-sizing: border-box} div{ width: 70%; margin: 100px auto} ul{list-style: none; overflow: hidden} .tabs li{ float: left; width: 100px; text-align: center; line-height: 30px; background: #f60; border: 1px solid #f60; border-bottom: none; cursor: pointer; } .tabs li:hover{ background: #fff; } .tabs li.active{ background: #fff; } .content{ position: relative; width: 400px; height: 300px; border: 1px solid #f60; border-top: none; } .content li{ width: 100%; height: 100%; position: absolute; padding: 10px; display: none; } </style></head><body><div><ul class="tabs" id="tabs"> <li class="active">選項1</li> <li>選項2</li> <li>選項3</li> <li>選項4</li></ul> <ul class="content" id="content"> <li style="display: block">內容1</li> <li>內容2</li> <li>內容3</li> <li>內容4</li> </ul></div><script>// 普通方法實現一個選項卡切換 var tabs =document.getElementById("tabs"); var tablist = tabs.children; var content =document.getElementById("content"); var conlist = content.children; for(i=0;i<tablist.length; i++){ tablist[i].index = i; // tablist[i]增加index屬性 ,屬性值為該項的下標 tablist[i].onclick = function(){ for(j=0;j<conlist.length;j++){ tablist[j].className =""; conlist[j].style.display = "none"; } this.className = "active"; conlist[this.index].style.display = "block"; } }</script></body></html>
3. 第三種實現效果為:
在第二種實現方式的基礎上,實現多個選項卡的切換,使用了閉包函數
主要實現代碼:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } div { margin: 100px; } ul { list-style: none; overflow: hidden; } .tabs li { float: left; width: 100px; height: 50px; line-height: 50px; text-align: center; border: 1px solid fuchsia; background: salmon; border-bottom: none; cursor: pointer; } .tabs li.active { background: beige } .content { position: relative; width: 400px; height: 300px; border: 1px solid fuchsia; border-top: none; } .content li { width: 100%; height: 100%; position: absolute; display: none; padding: 170px; } </style></head><body><div> <ul class="tabs" id="tab1"> <li class="active">選項1</li> <li>選項2</li> <li>選項3</li> <li>選項4</li> </ul> <ul class="content" id="content1"> <li style="display: block">內容1</li> <li>內容2</li> <li>內容3</li> <li>內容4</li> </ul></div><div> <ul class="tabs" id="tab2"> <li class="active">選項1</li> <li>選項2</li> <li>選項3</li> <li>選項4</li> </ul> <ul class="content" id="content2"> <li style="display: block">內容1</li> <li>內容2</li> <li>內容3</li> <li>內容4</li> </ul></div><script> function tab(tabid, contentid){ var tabul = document.getElementById(tabid); var tablists = tabul.getElementsByTagName("li"); var contentul = document.getElementById(contentid); var conlists = contentul.getElementsByTagName("li"); for(i=0;i<tablists.length; i++){ tablists[i].onclick = function(i){ //閉包函數 return function (){ for(j=0;j<conlists.length;j++){ tablists[j].className =""; conlists[j].style.display = "none"; } this.className = "active"; conlists[i].style.display = "block"; } }(i); } } tab("tab1", "content1"); tab("tab2", "content2");</script></body></html>
js實現選項卡切換的三種方式