原生js選項卡

js實現(xiàn)選項卡

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
       .box{
           width: 460px;
           height:500px;
           border: 1px solid red;
       }
        .menu{
            width: 100%
        }
        .menu span{
            display: inline-block;
            width: 150px;
            height: 30px;
            border: 1px solid blue;
            line-height: 30px;
            text-align: center;
            margin-right: -5px;
            cursor: pointer;
        }
        .menu span.active{
            background: lightseagreen;
            color: #fff;
        }
        .content{
            width: 100%;
            height: 468px;
        }
        .content div{
            width: 100%;
            height: 100%;
            border: 1px solid gray;
            display: none;
        }
        .content div:first-child{
            display: block;
        }
    </style>
</head>
<body>
<div class="box" id="box">
    <div class="menu">
        <span class="active">菜單1</span>
        <span>菜單1</span>
        <span>菜單1</span>
    </div>
    <div class="content">
        <div>第一個顯示</div>
        <div>第二個顯示</div>
        <div>第三個顯示</div>
    </div>
</div>
<script>
   /* var oBox =document.getElementById("box");
    var menu =oBox.querySelectorAll(".menu span");
    var content =oBox.querySelectorAll(".content div");
     for(var i =0;i<menu.length;i++){
         menu[i].setAttribute("data-index",i);
         menu[i].onclick=function(){
             for(var i =0;i<menu.length;i++){
                 menu[i].className="";
                 content[i].style.display="none";
             }
             this.className="active";
             var index =this.getAttribute("data-index");
             content[index].style.display="block";
         }
     }*/
   function Tab(json){
       this.oBox =document.getElementById(json.id);
       this.menu =this.oBox.querySelectorAll(".menu span");
       this.content =this.oBox.querySelectorAll(".content div");
       var _this =this;
       for(var i =0;i<this.menu.length;i++){
           this.menu[i].setAttribute("data-index",i);
           this.menu[i].onclick=function(){
               _this.init(this);
           };
       }
   }
   Tab.prototype.init=function(that){
       for(var i =0;i<this.menu.length;i++){
           this.menu[i].className="";
           this.content[i].style.display="none";
       }
       that.className="active";
       var index =that.getAttribute("data-index");
       this.content[index].style.display="block";
   }
   new Tab({
       id:"box"
   })
</script>
</body>
</html>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末腻格,一起剝皮案震驚了整個濱河市觉鼻,隨后出現(xiàn)的幾起案子拷获,更是在濱河造成了極大的恐慌到涂,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件碰缔,死亡現(xiàn)場離奇詭異预皇,居然都是意外死亡,警方通過查閱死者的電腦和手機古掏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來侦啸,“玉大人槽唾,你說我怎么就攤上這事∑ブ校” “怎么了夏漱?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長顶捷。 經常有香客問我挂绰,道長,這世上最難降的妖魔是什么服赎? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任葵蒂,我火速辦了婚禮,結果婚禮上重虑,老公的妹妹穿的比我還像新娘践付。我一直安慰自己,他們只是感情好缺厉,可當我...
    茶點故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布永高。 她就那樣靜靜地躺著,像睡著了一般提针。 火紅的嫁衣襯著肌膚如雪命爬。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天辐脖,我揣著相機與錄音饲宛,去河邊找鬼。 笑死嗜价,一個胖子當著我的面吹牛艇抠,可吹牛的內容都是我干的。 我是一名探鬼主播久锥,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼家淤,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了瑟由?” 一聲冷哼從身側響起媒鼓,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎错妖,沒想到半個月后绿鸣,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡暂氯,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年潮模,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片痴施。...
    茶點故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡擎厢,死狀恐怖,靈堂內的尸體忽然破棺而出辣吃,到底是詐尸還是另有隱情动遭,我是刑警寧澤,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布神得,位于F島的核電站厘惦,受9級特大地震影響,放射性物質發(fā)生泄漏哩簿。R本人自食惡果不足惜宵蕉,卻給世界環(huán)境...
    茶點故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望节榜。 院中可真熱鬧羡玛,春花似錦、人聲如沸宗苍。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽讳窟。三九已至让歼,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間挪钓,已是汗流浹背是越。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留碌上,地道東北人倚评。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像馏予,于是被迫代替她去往敵國和親天梧。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內容