js面向过程的选项卡

代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>选项卡</title>
    <style>
        body{
            background: #000;
        }
        div{
            width: 300px;
            height: 300px;
            border: 1px solid #000;
            display: none;
            border-radius: 30px;
            margin-top: 10px;
            color: #fff;
            text-align: center;
            line-height: 300px;
            font-size: 100px;
        }
        input{
            width: 100px;
            height: 40px;
            display: inline-block;
            cursor: pointer;
        }
    </style>
</head>
<body>
<section id="box">
    <input type="button" value="按钮1" style="background: hotpink">
    <input type="button" value="按钮2">
    <input type="button" value="按钮3">
    <div style="display: block;border-color: hotpink">1</div>
    <div>2</div>
    <div>3</div>
</section>

</body>
<script>
    window.onload=function () {

            //var obox=document.getElementById(id);
            var btns=document.getElementsByTagName('input');
            var oDiv=document.getElementsByTagName('div');

            for(var i=0;i<btns.length;i++){
                btns[i].index=i;//索引
                btns[i].onclick=function () {
                    for(var i=0;i<oDiv.length;i++){
                        btns[i].style.background='';//点击后先清除所有的按钮背景

                        oDiv[i].style.display='none';
                        oDiv[i].style.borderColor='hotpink';
                    }
                    oDiv[this.index].style.display='block';//this指当前点击的元素按钮
                    oDiv[this.index].style.borderColor='hotpink';
                    this.style.background='hotpink';//给当前按钮加背景
                }
            }
    }
</script>
</html>

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值