js面向对象---选项卡

本文探讨了如何使用JavaScript的面向对象编程思想来创建一个动态的选项卡组件。通过封装对象,实现了选项卡的切换效果,提高了代码的复用性和可维护性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            body{padding: 100px;}
            #box ,#box2{
                position: relative;
                margin-top: 30px;
                width: 550px;
                height: 550px;
            }
            .active{background: #f00;}
            #box div,#box2 div{
                border: 1px solid #ccc;
                width: 500px;
                height: 500px;
                position: absolute;
                top: 35px;
                left: 0;
                display: none;
            }
        </style>
    </head>
    <body>
        <div id="box">
            <input class="active" type="button" name="" id="" value="a" />
            <input type="button" name="" id="" value="b" />
            <input type="button" name="" id="" value="c" />
            <input type="button" name="" id="" value="d" />
            <div style="display: block;">1</div>
            <div>2</div>
            <div>3</div>
            <div>4</div>
        </div>
        <div id="box2">
            <input class="active" type="button" name="" id="" value="a" />
            <input type="button" name="" id="" value="b" />
            <input type="button" name="" id="" value="c" />
            <input type="button" name="" id="" value="d" />
            <div style="display: block;">1</div>
            <div>2</div>
            <div>3</div>
            <div>4</div>
        </div>
    </body>
    <!--面向过程-->

    <!--<script type="text/javascript">
        var oBox = document.getElementById('box');
        var aBtn = oBox.getElementsByTagName('input');
        var aDiv = oBox.getElementsByTagName('div');

        for(var i=0;i<aBtn.length;i++){
            aBtn[i].index = i;
            aBtn[i].onclick = fnSwitch;
        }       

        function fnSwitch(){
            for(var i=0;i<aBtn.length;i++){
                aBtn[i].className = "";
                aDiv[i].style.display = "none";
            }
            aBtn[this.index].className = "active";
            aDiv[this.index].style.display = "block";
        }
    </script>-->


    <!--面向过程改成面向对象的方法
    1、onload里面创建实例使用方法
    2、全局变量就是属性
    3、函数就是方法
    4、改变this指针,this指向object-->


    <!--面向对象-->
    <script type="text/javascript">
        //      调用 如果不写window.onload这个函数,调用要写到最后面
        window.onload = function(){
            var t1 = new Tab('box');
            t1.ints();
            t1.auto();
            var t2 = new Tab('box2');
            t2.ints();
            t2.auto();
        }

        //  构造函数,设置属性
        function Tab(id){       
            this.oBox = document.getElementById(id);  //传参数
            this.aBtn = this.oBox.getElementsByTagName('input');
            this.aDiv = this.oBox.getElementsByTagName('div');
            this.now = 0;
        }


        // 原型,设置方法
        Tab.prototype.ints = function(){
            var _this = this;
            for(var i=0;i<this.aBtn.length;i++){
                this.aBtn[i].index = i;
                this.aBtn[i].onclick = function(){
                    _this.fnSwitch(this);   //this改变指针,传参
                } ;
            }
        }

        //选项卡事件
        Tab.prototype.fnSwitch = function (oBtn){
            for(var i=0;i<this.aBtn.length;i++){
                this.aBtn[i].className = "";
                this.aDiv[i].style.display = "none";
            }
            oBtn.className = "active";
            this.aDiv[oBtn.index].style.display = "block";
        }



        //自动轮播选项卡
        Tab.prototype.auto = function(){
            var _this = this;
            setInterval(function(){
                _this.now++;
                if(_this.now ==  _this.aBtn.length){
                    _this.now = 0;
                }
                for(var i=0;i<_this.aBtn.length;i++){
                    _this.aBtn[i].className = "";
                    _this.aDiv[i].style.display = "none";
                }
                _this.aBtn[_this.now].className = "active";
                _this.aDiv[_this.now].style.display = "block";  

            },3000)
        }
    </script>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值