tab选项卡

11 篇文章 0 订阅
1 篇文章 0 订阅

第一种

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .box{
            width: 400px;
            height: 400px;
            margin: 100px auto;
        }
        .box .header span{
            width: 96px;
            height: 45px;
            line-height: 45px;
            display: inline-block;
            text-align: center;
            cursor: pointer;
            background-color: pink;
        }
        .box .header span.selected1{
            background-color: cyan;
        }
        .content li{
            list-style: none;
            height: 250px;
            background-color: orange;
            display: none;
        }
        .content li.select1{
            display: block;
        }
    </style>
</head>
<body>
<div class="box" id="box">
    <div class="header">
        <span class="selected1">体育</span>
        <span>娱乐</span>
        <span>新闻</span>
        <span>综合</span>
    </div>
    <div class="content">
        <ul>
            <li class="select1">我是体育</li>
            <li>我是娱乐</li>
            <li>我是新闻</li>
            <li>我是综合</li>
        </ul>
    </div>
</div>
<script src="common.js"></script>
<script>
    //获取所有的li标签
    var list = document.getElementsByTagName("li");
    //获取所有的span标签
    var spans = document.getElementsByTagName("span");
    //遍历点击事件
    for ( var i = 0;i < spans.length; i++){
        //在点击之前,把索引值保存到span标签
        spans[i].setAttribute("index", i)
        spans[i].onmouseover = function () {
            //1.把所有的span样式全部移除
            for ( var j = 0; j < spans.length; j++){
                spans[j].removeAttribute("class");
            }
            this.className = "selected1";
            //3.获取span的索引值
            var num = this.getAttribute("index");
            //4.先让所有的li全部隐藏
            for ( var k = 0; k < spans.length; k++){
                list[k].removeAttribute("class");
            }
            //5.让当前被点击的span对应的li显示
            list[num].className = "select1";
        }
    }
</script>
</body>
</html>

第二种

样式表

<style>
.center{
            width: 600px;
            height: 500px;
            border: 1px solid #ccc;
            margin: 0 auto;
            background-color: #eee;
        }
        .cen_top{
            width: 600px;
            height: 90px;
        }
        .cen_main {
            width: 600px;
            height: 410px;
            /*overflow: hidden;*/
        }
        .cen_top div{
            width: 25%;
            height: 90px;
            line-height: 90px;
            float: left;
            font-size: 18px;
            text-align: center;
            cursor: pointer;
            font-weight: bold;
        }
        .cen_main div{
            width: 600px;
            height: 410px;
            line-height: 410px;
            text-align: center;
            font-size: 24px
        }
    </style>

主体内容

<div class="center">
    <div id="d1" class="cen_top">
        <div style="background-color: white;">选项卡1</div>
        <div>选项卡2</div>
        <div>选项卡3</div>
        <div>选项卡4</div>
    </div>
    <div id="d2" class="cen_main">
        <div style="background-color: white;color: blue;">选项卡1的正文内容</div>
        <div style="display: none;color: purple;">选项卡2的正文内容</div>
        <div style="display: none;color: deeppink;">选项卡3的正文内容</div>
        <div style="display: none;color: green;">选项卡4的正文内容</div>
    </div>
</div>

js代码

function my$(id){
    return document.getElementById(id);
}


var dvs = my$("d1").getElementsByTagName("div");
    var dvs2 = my$("d2").getElementsByTagName("div");
    for ( var i = 0; i < dvs.length; i++){
        dvs[i].index = i;	//获取索引
        dvs[i].onclick = function () {
            for (var j = 0; j < dvs.length; j++){
            //将所有的改为默认
                dvs[j].style.backgroundColor = "#eee";
                dvs2[j].style.display = "none";
                dvs2[j].style.backgroundColor = "#eee";
            }
            //谁点击就赋给谁
            this.style.backgroundColor = "white";
            dvs2[this.index].style.display = "block";
            dvs2[this.index].style.backgroundColor = "white";
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值