前兩天寫了一個簡易的選項卡 封裝一下 暫時沒發現有什麼好處 等之後作的東西多了再說css
<!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>
csshtml
*{ 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; }
jsapp
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(); }
練習使用沒啥看頭ui