js選項卡的實現方法,js選項卡實現方法

來源:互聯網
上載者:User

js選項卡的實現方法,js選項卡實現方法

本文執行個體講述了js選項卡的實現方法。分享給大家供大家參考。具體分析如下:

一、思路

1. 擷取元素;

2. for迴圈按鈕元素添加onclick(點擊) 或者 onmousemove(移入)事件;

3. 點擊當前按鈕時會以高亮狀態顯示,通過for迴圈曆遍把所有的按鈕樣式設定為空白在把所有div的display設定為none。

4. 點擊當前按鈕添加樣式,把當前div顯示出來,display設定為block。

二、html代碼:

<div id="div1">    <input type="button" class="active" value="1"/>    <input type="button" value="2"/>    <input type="button" value="3"/>     <input type="button" value="4"/>      <div class="div2" style="display:block;">11</div>      <div class="div2">22</div>      <div class="div2">33</div>      <div class="div2">44</div></div>

三、css部分:

.active{background:#9CC;}.div2{width:300px;height:200px; border:1px #666666 solid;display:none;}

四、js代碼:

<script>window.onload=function(){   var odiv=document.getElementById('div1');//擷取div   var btn=odiv.getElementsByTagName('input');//擷取div下的input   var div2=odiv.getElementsByTagName('div') ;//擷取div下的div  for(i=0;i<btn.length;i++)//迴圈每個按鈕   {      btn[i].index=i //btn[i]是指定button的第i個按鈕;為該按鈕添加一個index屬性,並將index的值設定為i     btn[i].onclick=function()//按鈕的第i個點擊事件    {    for(i=0;i<btn.length;i++)//迴圈去掉button的樣式,把div隱藏     {        btn[i].className='' //清空按鈕的樣式       div2[i].style.display='none'//隱藏div      }        this.className='active'//自身添加active        div2[this.index].style.display='block'//this.index是當前div     }   }}</script>

希望本文所述對大家的javascript程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.