国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

js+css實現tab菜單切換效果的方法

2019-11-20 13:21:38
字體:
來源:轉載
供稿:網友

本文實例講述了js+css實現tab菜單切換效果的方法。分享給大家供大家參考。具體實現方法如下:

index.css如下:

復制代碼 代碼如下:
* { 
    margin: 0px; 
    padding: 0px; 

body { 
    width: 600px; 
    margin: 0 auto; 
    background-color: silver; 

 
#contanier { 
    background-color: yellow; 
    width: 600px;height: 400px; 

 
#tabNavi { 
    width: 600px;height: 30px; 
    background-color: #00bfff; 
    text-decoration: none; 
    list-style-type: none; 

 
#tabNavi li { 
    float: left; 
    margin-right: 7px; 
     background-color: #008b8b; 
    color: white; 
    cursor: pointer; 
    width: 60px; 
    height: 28px; 
    line-height: 30px; 
    text-align: center; 

#content { 
    width: 600px;height: 370px; 
    background-color: white; 
}

index.html如下:

復制代碼 代碼如下:
<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
    <title>js實現tab菜單動態(tài)切換效果</title> 
    <link href="css/index.css" rel="stylesheet" /> 
    <script type="text/javascript"> 
        function gel(id) { 
            return document.getElementById(id); 
        } 
 
        var arr = [ 
            { "title": "新聞", "content": "這是新聞頻道" }, 
            { "title": "財經", "content": "這是財經頻道" }, 
            { "title": "娛樂", "content": "這是娛樂頻道" }, 
            { "title": "體育", "content": "這是體育頻道" }, 
            { "title": "汽車", "content": "這是汽車頻道" }, 
            { "title": "視頻", "content": "這是視頻頻道" }, 
            { "title": "生活", "content": "這是生活頻道" } 
        ]; 
 
        window.onload = function() { 
            for (var i = 0; i < arr.length; i++) { 
                var liNew = document.createElement("li"); 
                liNew.innerHTML = arr[i].title; 
                gel("tabNavi").appendChild(liNew); 
                //在這些li上面都綁定點擊事件,就需要給他們每一個賦一個屬性(最好是id) 
                liNew.setAttribute("id", i); 
                 
                liNew.onclick = function () { 
                   var navs = gel("tabNavi").childNodes; 
                    //清除所有顏色 
                   for (var j = 0; j < navs.length; j++) { 
                       if (navs[j].nodeType == 1) { 
                           navs[j].style.backgroundColor ="#008b8b"; 
                       } 
                    } 
 
                    this.style.backgroundColor = "red"; 
                    gel("content").innerHTML = arr[this.id].content; 
                }; 
            } 
        }; 
    </script> 
</head> 
<body> 
    <div id="contanier"> 
        <ul id="tabNavi"></ul> 
        <div id="content" class="content"></div> 
    </div> 
</body> 
</html>

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 北票市| 上饶市| 二连浩特市| 车致| 明光市| 涡阳县| 五河县| 怀柔区| 十堰市| 阿拉善盟| 许昌县| 富顺县| 玉龙| 寻甸| 庆阳市| 名山县| 鹤壁市| 博野县| 招远市| 道孚县| 家居| 彭州市| 阿图什市| 英山县| 雅江县| 广平县| 乌鲁木齐县| 黄浦区| 双柏县| 望谟县| 阿勒泰市| 西林县| 广德县| 深水埗区| 什邡市| 西昌市| 松潘县| 攀枝花市| 肥乡县| 贞丰县| 石棉县|