• 面向对象小实例之 选项卡


    直接上代码 :

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
    #wrap1,
    #wrap2,{
    float: left;
    }
    div div{
    border: 1px solid #000;
    200px;
    height: 50px;
    font-size: 20px;
    text-align: center;
    line-height: 50px;
    display: none;
    }
    .active{
    background: red;
    }
    </style>
    </head>
    <body>
    </body>
    <div id="wrap1">
    <input type="button" value="aaa" class="active">
    <input type="button" value="bbb">
    <input type="button" value="ccc">
    <div style="display: block;">aaa</div>
    <div>bbb</div>
    <div>ccc</div>
    </div>
    <div id="wrap2">
    <input type="button" value="aaa" class="active">
    <input type="button" value="bbb">
    <input type="button" value="ccc">
    <div style="display: block;">aaa</div>
    <div>bbb</div>
    <div>ccc</div>
    </div>

    <script type="text/javascript">

    //原本面向过程的写法是酱紫的:
    /*var btn=document.getElementsByTagName("input");
    var div=document.getElementsByTagName("div");
    for(var i=0;i<btn.length;i++){
    btn[i].index=i;
    btn[i].onclick=function(){
    for(var i=0;i<btn.length;i++){
    btn[i].className='';
    div[i].style.display='none';
    }
    this.className='active';
    div[this.index].style.display='block';
    }

    }*/

    //后来,面向对象时酱紫的 :
    function Tab(obj){
    this.parentNode=document.getElementById(obj)
    this.btn=this.parentNode.getElementsByTagName("input");
    this.div=this.parentNode.getElementsByTagName("div");
    this.init();
    };
    Tab.prototype.init=function(){
    var _this=this;
    for(var i=0;i<this.btn.length;i++){
    this.btn[i].index=i;
    this.btn[i].onclick=function(){
    _this.clickFn(this);
    };
    }
    };
    Tab.prototype.clickFn = function(_this){
    for(var i=0;i<this.btn.length;i++){
    this.btn[i].className='';
    this.div[i].style.display='none';
    }
    _this.className='active';
    this.div[_this.index].style.display='block';
    };
    Tab.prototype.autoPlay = function(){
    this.n=0;
    var _this=this;
    setInterval(function(){
    _this.n++;
    if(_this.n ==_this.btn.length){
    _this.n=0;
    }
    for(var i=0;i<_this.btn.length;i++){
    _this.btn[i].className='';
    _this.div[i].style.display='none';
    }
    _this.btn[_this.n].className='active';
    _this.div[_this.n].style.display='block';
    },1000);


    };
    var t1=new Tab("wrap1");
    var t2=new Tab("wrap2");
    t1.autoPlay();
    t2.autoPlay();
    </script>
    </html>

  • 相关阅读:
    文件上传-pubsec-文件上传大小限制
    编写 .gitignore 文件
    Git 创建点开头的文件和目录
    Git 克隆远程仓库到本地
    redis 在 windows 中的安装
    查看数据库字符集和排序规则
    centos 6 和centos 7 系统下vnc配置
    centos 6 下KVM 安装学习之旅
    Centos 下使用VLAN+Bridge 搭建KVM基础网络环境
    centos 6 KVM 网卡桥接配置
  • 原文地址:https://www.cnblogs.com/week-1/p/6545395.html
Copyright © 2020-2023  润新知