每日一个js实例7--通过面向对象实现选项卡

<div id="box">
        <input type="button" value="1" class="active">
        <input type="button" value="2">
        <input type="button" value="3">
        <div style="display:block">1</div>
        <div>2</div>
        <div>3</div>

    </div>

<style>
            #box input{background: gray;}
            #box div{width: 200px;height: 200px;border: 1px solid red;display: none}
            #box .active{background: gold;}
</style>


<script>
    //第一种写法将属性包装入构造函数中
        // function Tab(id){
        //     //面向对象中属性可称为变量,方法称为函数
        //     this.oBox=document.getElementById(id);
        //     this.aInput=this.oBox.getElementsByTagName("input");
        //     this.aDiv=this.oBox.getElementsByTagName("div");
        //     for(var i=0;i<this.aInput.length;i++){
        //         this.aInput[i].index=i;
        //         var _this=this;
        //         this.aInput[i].οnclick=function(){
        //             for(var i=0;i<_this.aInput.length;i++){
        //                 _this.aInput[i].className="";
        //                 _this.aDiv[i].style.display="none";
        //             }
        //             _this.aInput[this.index].className="active";
        //             _this.aDiv[this.index].style.display="block";
        //         }
        //     }
        // }
        // var tab=new Tab("box");

        //第二种方法,属性在构造函数中申明,方法在原型对象中申明
        // function Tab(id){
        //     //面向对象中属性可称为变量,方法称为函数
        //     this.oBox=document.getElementById(id);
        //     this.aInput=this.oBox.getElementsByTagName("input");
        //     this.aDiv=this.oBox.getElementsByTagName("div");
        //     //this.draw();//在此调用可以
        // }
        // Tab.prototype.draw=function(){
        //     constructor:Tab;
        //     for(var i=0;i<this.aInput.length;i++){
        //         this.aInput[i].index=i;
        //         var _this=this;
        //         this.aInput[i].οnclick=function(){
        //             for(var i=0;i<_this.aInput.length;i++){
        //                 _this.aInput[i].className="";
        //                 _this.aDiv[i].style.display="none";
        //             }
        //             _this.aInput[this.index].className="active";
        //             _this.aDiv[this.index].style.display="block";
        //         }
        //     }
        // }

        // var tab=new Tab("box");
        // tab.draw();//也可以再次调用

         //第三种写法,属性与方法的一部分在构造函数中申明,方法另一部分在原型对象中申明
        // function Tab(id){
        //     //面向对象中属性可称为变量,方法称为函数
        //     this.oBox=document.getElementById(id);
        //     this.aInput=this.oBox.getElementsByTagName("input");
        //     this.aDiv=this.oBox.getElementsByTagName("div");
        //     for(var i=0;i<this.aInput.length;i++){
        //         this.aInput[i].index=i;
        //         var _this=this;
        //         this.aInput[i].οnclick=function(){
        //            _this.draw(this.index);
        //         }
        //     }
        // }
        // Tab.prototype.draw=function(index){
        //      for(var i=0;i<this.aInput.length;i++){
        //                 this.aInput[i].className="";
        //                 this.aDiv[i].style.display="none";
        //             }
        //             this.aInput[index].className="active";
        //             this.aDiv[index].style.display="block";
        // }
        // var tab=new Tab("box");

         //第四种写法,属性与方法全部在构造函数中申明
        function Tab(id){
            this.oBox=document.getElementById(id);
            this.aInput=this.oBox.getElementsByTagName("input");
            this.aDiv=this.oBox.getElementsByTagName("div");
            this.draw=function(){
                  for(var i=0;i<this.aInput.length;i++){
                    this.aInput[i].index=i;
                    var _this=this;
                    this.aInput[i].οnclick=function(){
                        for(var i=0;i<_this.aInput.length;i++){
                            _this.aInput[i].className="";
                            _this.aDiv[i].style.display="none";
                        }
                        _this.aInput[this.index].className="active";
                        _this.aDiv[this.index].style.display="block";
                    }
                }
            }
            this.draw();
        }
        var tab=new Tab("box");
        //tab.draw();
    </script>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值