JavaScript-选项卡切片效果

    <style>
        #box1,#box2{
            width:300px;
            height:400px;
        }
        h3{
            height:40px;
            line-height: 40px;
        }
        h3>a{
            width:50px;
            height:40px;
            display: inline-block;
        }
        h3 a.active,h3 a:hover{
            background: #eee;
            color:#900;
        }
        #box1>.con,#box2>.con{
            width:300px;
            height:350px;
            border:solid 1px #000;
            position: relative;
        }
        #box1>.con>div,#box2>.con>div{
            width:300px;
            height:350px;
            position: absolute;
            top:0;
            left:0;
            display: none; /*隐藏所有div*/
        }
    </style>
</head>
<body>
<div id="box1">
    <h3 class="titles">
        <a href="javascript:void(0)" class="active">标题1</a>
        <a href="javascript:void(0)">标题2</a>
        <a href="javascript:void(0)">标题3</a>
        <a href="javascript:void(0)">标题4</a>
    </h3>
    <div class="con">
        <div>内容1</div>
        <div>内容2</div>
        <div>内容3</div>
        <div>内容4</div>
    </div>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div id="box2">
    <h3 class="titles">
        <a href="javascript:void(0)" class="active">标题1</a>
        <a href="javascript:void(0)">标题2</a>
        <a href="javascript:void(0)">标题3</a>
        <a href="javascript:void(0)">标题4</a>
    </h3>
    <div class="con">
        <div>内容1</div>
        <div>内容2</div>
        <div>内容3</div>
        <div>内容4</div>
    </div>
</div>
  1. 显示第一个div;
  2. 鼠标悬浮在每一个a上时;/a[x].index=x;/a[x].οnmοuseοver=function
  3. 每个div隐藏起来;/显示当前div
<script>
    window.onload=function(){
        tabs("box2");
        tabs("box1");
    }
    var as=document.getElementsByClassName("titles")[0].children; //define as=h3>a
    var cons=document.getElementsByClassName("con")[0].children;//define cons=div>div
    cons[0].style.display="block"; console.log("defult show cons[0]");//display cons[0]=div.1
    for(var x=0;x<as.length;x++){ //every as[x] funcition
        as[x].index=x;//index=as[x].x
        as[x].onmouseover=function(){
            console.log(this.index); // x always=4; this.index在改变
            for(var y=0;y<cons.length;y++){
                cons[y].style.display="none";//none all div
            } console.log(this.index);//show this=cons[index] div
              cons[this.index].style.display="block";
        }
    }
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值