JavaScript實(shí)現(xiàn)tab欄切換效果
本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)tab欄切換效果的具體代碼,供大家參考,具體內(nèi)容如下
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <title>Document</title> <!-- css樣式 --> <style> * { margin: 0; padding: 0; } .box { width: 600px; margin: 100px 500px; border: 1px solid #999; } li { list-style: none; } .tab_top { width: 600px; height: 50px; background-color: #ccc; } .tab_top li { float: left; width: 50px; height: 50px; line-height: 50px; text-align: center; padding: 0 20px; } .current { background-color: red; color: #fff; } .tab_con { width: 600px; height: 300px; background-color: #fff; } 先在css里面設(shè)置下面小div全部隱藏 .item { display: none } </style> <!-- html結(jié)構(gòu) --> <body> <div class='box'> <div class='tab_top'> <ul> <li class='current'>標(biāo)題一</li> <li>標(biāo)題二</li> <li>標(biāo)題三</li> <li>標(biāo)題四</li> <li>標(biāo)題五</li> </ul> </div> <div class='tab_con'> <div style='display: block;'> --->此處默認(rèn)第一個(gè)顯示 我是內(nèi)容一<br> 我是內(nèi)容一<br> 我是內(nèi)容一<br> 我是內(nèi)容一<br> 我是內(nèi)容一<br> </div> <div class='item'> 我是內(nèi)容二<br> 我是內(nèi)容二<br> 我是內(nèi)容二<br> 我是內(nèi)容二<br> 我是內(nèi)容二<br> </div> <div class='item'> 我是內(nèi)容三<br> 我是內(nèi)容三<br> 我是內(nèi)容三<br> 我是內(nèi)容三<br> 我是內(nèi)容三<br> </div> <div class='item'> 我是內(nèi)容四<br> 我是內(nèi)容四<br> 我是內(nèi)容四<br> 我是內(nèi)容四<br> 我是內(nèi)容四<br> </div> <div class='item'> 我是內(nèi)容五<br> 我是內(nèi)容五<br> 我是內(nèi)容五<br> 我是內(nèi)容五<br> 我是內(nèi)容五<br> 我是內(nèi)容五<br> </div> </div> </div></body>
js:
<script> //1 上的選項(xiàng)卡 點(diǎn)擊某一個(gè) 當(dāng)前這一個(gè)底色是紅色 其余不變 (排他思想) 修改類(lèi)名的方法 //獲取元素 //01 獲取上半部分盒子 var tab_top = document.querySelector(’.tab_top’); //02 獲取上班部分 所有小li 得到一個(gè)數(shù)組 var lis = tab_top.querySelectorAll(’li’); //03 獲取下半部分 所有小div盒子 得到一個(gè)數(shù)組 var item = document.querySelectorAll(’.item’) //for 循環(huán)所有小li 綁定點(diǎn)擊事件 for (var i = 0; i < lis.length; i++) { //開(kāi)始給上面li設(shè)置索引號(hào) 屬性index 值 i lis[i].setAttribute(’index’, i); // 注冊(cè)點(diǎn)擊事件 lis[i].onclick = function () { // 來(lái)個(gè)排他思想 for (var j = 0; j < lis.length; j++) { //先讓所有的li 點(diǎn)擊沒(méi)有樣式 lis[j].className = ’’; } // 誰(shuí)點(diǎn)擊 誰(shuí)加樣式 this.className = ’current’ //2 下面顯示內(nèi)容模塊也要寫(xiě)在onclick里面 因?yàn)橐灰粚?duì)應(yīng) // 給上面top 所有l(wèi)i 添加index 索引號(hào) 屬性從0開(kāi)始 自定義屬性 // 上面已經(jīng)設(shè)置好 現(xiàn)在拿來(lái)用 var index = this.getAttribute(’index’); // 點(diǎn)擊上面li 對(duì)應(yīng)下面div顯示出來(lái) //再來(lái)排他思想 先干掉其他人 讓隱藏 點(diǎn)誰(shuí) 誰(shuí)顯示 for (var k = 0; k < item.length; k++) { //所有的小div 隱藏 item[k].style.display = ’none’ } //點(diǎn)擊哪個(gè)小li 小li的index對(duì)應(yīng)的div 顯示 item[index].style.display = ’block’; } }</script>
效果圖:
如果大家還想深入學(xué)習(xí),可以點(diǎn)擊兩個(gè)精彩的專題:javascript選項(xiàng)卡操作方法匯總 jquery選項(xiàng)卡操作方法匯總
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. XML入門(mén)的常見(jiàn)問(wèn)題(三)2. XML 非法字符(轉(zhuǎn)義字符)3. .NET Framework各版本(.NET2.0 3.0 3.5 4.0)區(qū)別4. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)5. 關(guān)于html嵌入xml數(shù)據(jù)島如何穿過(guò)樹(shù)形結(jié)構(gòu)關(guān)系的問(wèn)題6. 前端html+css實(shí)現(xiàn)動(dòng)態(tài)生日快樂(lè)代碼7. 不要在HTML中濫用div8. el-input無(wú)法輸入的問(wèn)題和表單驗(yàn)證失敗問(wèn)題解決9. JSP取得在WEB.XML中定義的參數(shù)10. WMLScript的語(yǔ)法基礎(chǔ)
