一個(gè)簡(jiǎn)單的選項(xiàng)卡的關(guān)鍵在于:當(dāng)切換頁(yè)面時(shí),如何讓所選的選項(xiàng)和與其對(duì)應(yīng)的內(nèi)容同時(shí)出現(xiàn),并且在選擇其他的內(nèi)容時(shí),不影響新的內(nèi)容的顯示。 
其中用到兩個(gè)很關(guān)鍵的思想: 
1.為對(duì)象增加index屬性,并通過(guò)this對(duì)index 的調(diào)用來(lái)使每個(gè)選項(xiàng)能顯示出所對(duì)應(yīng)的內(nèi)容,并且屬性值要設(shè)置為循環(huán)時(shí)的數(shù)值-i。 
2.通過(guò)對(duì)class(類(lèi))的靈活使用,來(lái)改變當(dāng)先所選中目標(biāo)的樣式。 
3、用for循環(huán)嵌套事件對(duì)每一項(xiàng)進(jìn)行遍歷。 
4、在編譯時(shí),位于body中的div和input要有預(yù)先定義的行內(nèi)樣式或者信息。 
5、button和div.style.display要同時(shí)清零,同時(shí)出現(xiàn)才能達(dá)到選項(xiàng)卡的目的。
代碼:
<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <title>選項(xiàng)卡</title>  <style type="text/css">     #div1 .active{      background: red;      color: white;    }    #div1 div{      width: 237px;      height:150px;      background:#CCC;      display: none;    }  </style>  <script type="text/javascript">    window.onload=function(){      var oDiv = document.getElementById('div1');      var btn = oDiv.getElementsByTagName('input');      var aDiv = oDiv.getElementsByTagName('div');      for (var i = 0; i < btn.length; i++) {        btn[i].index=i;        //給btn增加一個(gè)index的屬性        btn[i].onmouseover = function(){          for (var i = 0; i < btn.length; i++) {            btn[i].className = '';            aDiv[i].style.display = 'none';          }          this.className = 'active';          aDiv[this.index].style.display = 'block';          //調(diào)用index屬性        }      }    };  </script></head><body>  <div id="div1">    <input type="button" value="首頁(yè)" class="active">    <input type="button" value="菜單">    <input type="button" value="幫助">    <input type="button" value="聯(lián)系">    <input type="button" value="贊助">    <div style="display: block;">這是首頁(yè)</div>    <div>這是菜單</div>    <div>這是幫助</div>    <div>這是聯(lián)系</div>    <div>這是贊助</div>  </div></body></html>以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注