1.做一个简单的选项卡
做一个简单的选项卡,类似于天气周一是什么天气周二是什么天气,点击哪天哪天下面的面板就会出现相应的内容。
html内容为
<!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;
}
javascript的内容为
window.onload = function(){
toJson = {
lable : "li",
topValue: ["周一","周二","周三","周四","周五","周六","周日"],
buttonValue: [
"气温变化平稳,建议穿薄型T恤短裤等夏装",
"昼夜温差极大,极易发生感冒,请特别注意增减衣服保暖防寒",
"近期空气干燥,注意经常补充水分,以保身体健康",
"请各单位、各部门对所在教学楼、办公楼积雪进行清理",
"凡在南阳台安装自来水的住户,请保护好水管,以免水管冻裂产生不必要的麻烦",
"扣紧门窗,免被强风吹开",
"雾霾天气,请减少外出"
],
topchange: "ok"
}
var top = document.getElementById("top");
var button = document.getElementById("button");
function ChangeCard(toJson) {
// 生成顶部和下面
for(i = 0; i<toJson.topValue.length;i++){
txt = "<" + toJson.lable + ">" + toJson.topValue[i] + "</" + toJson.lable + ">";
top.innerHTML += txt;
top.children[i].setAttribute("index",i);
}
// 生成按钮悬着
for(i = 0; i<toJson.topValue.length;i++){
top.children[i].onclick = function(){
for(i = 0; i<toJson.topValue.length;i++){
top.children[i].classList.remove("oks");
}
this.classList.add("oks");
button.innerHTML = toJson.buttonValue[this.getAttribute("index")]
}
}
}
ChangeCard(toJson)
}
在和后端的交互中,后端只需要传入toJson这个数组就能完成选项卡的变化
效果大致为


ps:记录学习使用,望各位大佬不喜勿喷,欢迎多多交流
qq:1759435876 微信邮箱同号

浙公网安备 33010602011771号