3.封装选项卡

时间:2021-08-09
本文章向大家介绍3.封装选项卡,主要包括3.封装选项卡使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

前两天写了一个简易的选项卡 封装一下  暂时没发现有什么好处  等以后做的东西多了再说

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>选项卡</title>
    <link rel="stylesheet" href="111.css">
    <script src="111.js"></script>
</head>
<body>
    <div class="ChangeCard">
        <div id="top">
            
        </div>
        <div id="button">
           
        </div>
    </div>   
</body>
</html>

css

*{
    margin: 0;
    list-style: none;
}
.ChangeCard{
    width: 300px;
    height: 100px;
    background-color: red;
    margin: 100px auto;
}
#top{
    /* margin: 0 auto; */
    height: 30px;
    line-height: 30px;
    background-color: yellowgreen;
    text-align: center;
}
#top li{
    display: inline;  
    margin: 0 5px;
    cursor: pointer;
}
#button{
    margin-top: 25px;
    text-align: center;
}
.oks{
    color: white;
}
.oksbox{
    display: none;
}

js

window.onload = function(){
    // 用面向对象封装选项卡  
    // 要求:传入参数周一周二周三周四周五周六周日的天气 
    // 当点击相应的选项卡 下方的button会变成对应的内容
    // 参数
    toJson = {
        lable : "li",
        topValue: ["周一","周二","周三","周四","周五","周六","周日"],
        buttonValue: [
            "气温变化平稳,建议穿薄型T恤短裤等夏装",
            "昼夜温差极大,极易发生感冒,请特别注意增减衣服保暖防寒",
            "近期空气干燥,注意经常补充水分,以保身体健康",
            "请各单位、各部门对所在教学楼、办公楼积雪进行清理",
            "凡在南阳台安装自来水的住户,请保护好水管,以免水管冻裂产生不必要的麻烦",
            "扣紧门窗,免被强风吹开",
            "雾霾天气,请减少外出"
        ],
        topchange: "ok"
    }
    function ChangeCard(toJson){
        topCard = document.getElementById("top");
        buttonCard = document.getElementById("button");
        // 创建内容模块
        this.createCard = function(){
            for(i=0;i<toJson.topValue.length;i++){
                oli = document.createElement("li");
                oli.setAttribute("index",i);
                topCard.appendChild(oli);
            }
        }
        
        // 点击事件效果模块
        this.EventClick = function(){
            topCard.children[0].style.color = "white";
            buttonCard.innerHTML = toJson.buttonValue[0]
            for(i=0;i<toJson.topValue.length;i++){
                // console.log(111);
                topCard.children[i].innerHTML = toJson.topValue[i];
                topCard.children[i].onclick = function(){
                    for(i=0;i<toJson.topValue.length;i++){
                        topCard.children[i].style.color = "black";
                    }
                    this.style.color = "white"
                    buttonCard.innerHTML = toJson.buttonValue[this.getAttribute("index")];
                }
            }
        };
        
    }
    changeCard = new ChangeCard(toJson);
    changeCard.createCard();
    changeCard.EventClick();
}

练习使用没啥看头

原文地址:https://www.cnblogs.com/fuyi2345/p/15120433.html