zoukankan      html  css  js  c++  java
  • 面向对象-选项卡

    普通函数改写为构造函数。

    前提:

    1.所有的东西都在onload里;

    2.不能有函数嵌套,可以有全局变量;

    改写过程:

    普通函数 构造函数
    onload(初始化整个程序) 构造函数(初始化一个对象)
    全局对象 变为对象的属性
    全局函数 变为对象的方法
    <script type="text/javascript">
        window.onload = function () {
            var oTab = new TabSwitch("div2");
        }
        var aBtn = null;
        var aDiv = null;
        function TabSwitch(id){
            var div2 = document.getElementById(id);
            this.aBtn = div2.getElementsByTagName('input');//全局变量 转变成属性
            this.aDiv = document.getElementsByClassName('box');
            for(i=0;i<this.aBtn.length;i++){
                this.aBtn[i].index = i;
                this.aBtn[i].onclick = this.tab;//是当前对象的方法,所以给tab添加this
    
            }
        }
        TabSwitch.prototype.tab = function (){//函数 转变成对象的方法
            for(i=0;i<this.aBtn.length;i++){
                this.aBtn[i].className = '';
                this.aDiv[i].style.display = 'none';
            }
            this.aDiv[this.index].style.display = 'block';
            this.className = 'active';
        }
    </script>

    3. 解决this问题:

    <script type="text/javascript">
        window.onload = function () {
            var oTab = new TabSwitch("div2");
        }
        var aBtn = null;
        var aDiv = null;
        function TabSwitch(id){
            var div2 = document.getElementById(id);
            this.aBtn = div2.getElementsByTagName('input');//全局变量 转变成属性
            this.aDiv = document.getElementsByClassName('box');
            var _this =this;//将这里的对象this存入_this中,方便在按钮点击里面用
            for(i=0;i<this.aBtn.length;i++){
                this.aBtn[i].index = i;
                this.aBtn[i].onclick = function () {
                    _this.tab(this);//这里的this指的是按钮,把它作为参数传到函数中;
                }
            }
        }
        TabSwitch.prototype.tab = function (aBtn){//函数 转变成对象的方法
    //        alert(this);  //input
    //        alert(this);    //这里的this指的是对象object;
            for(i=0;i<this.aBtn.length;i++){
                this.aBtn[i].className = '';
                this.aDiv[i].style.display = 'none';
            }
            aBtn.className = 'active';
            this.aDiv[aBtn.index].style.display = 'block';
        }
    </script>

    最后:完整案例:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>面向对象-选项卡</title>
        <style>
            .box{display:none;}
        </style>
    </head>
    <body>
    <div id="div2">
        <input class="active" type="button" value="王立娟">
        <input type="button" value="同学">
        <input type="button" value="几点上课">
    </div>
    <div class="box" style="display:block;">11</div>
    <div class="box">22</div>
    <div class="box">33</div>
    </body>
    </html>
    <script type="text/javascript">
        window.onload = function () {
            var oTab = new TabSwitch("div2");
        }
        var aBtn = null;
        var aDiv = null;
        function TabSwitch(id){
            var div2 = document.getElementById(id);
            this.aBtn = div2.getElementsByTagName('input');//全局变量 转变成属性
            this.aDiv = document.getElementsByClassName('box');
            var _this =this;//将这里的对象this存入_this中,方便在按钮点击里面用
            for(i=0;i<this.aBtn.length;i++){
                this.aBtn[i].index = i;
                this.aBtn[i].onclick = function () {
                    _this.tab(this);//这里的this指的是按钮,把它作为参数传到函数中;
                }
            }
        }
        TabSwitch.prototype.tab = function (aBtn){//函数 转变成对象的方法
    //        alert(this);  //input
    //        alert(this);    //这里的this指的是对象object;
            for(i=0;i<this.aBtn.length;i++){
                this.aBtn[i].className = '';
                this.aDiv[i].style.display = 'none';
            }
            aBtn.className = 'active';
            this.aDiv[aBtn.index].style.display = 'block';
        }
    </script>
  • 相关阅读:
    圆圈中最后剩下的数字 【微软面试100题 第十八题】
    第一个只出现一次的字符 【微软面试100题 第十七题】
    从上往下打印二叉树 【微软面试100题 第十六题】
    二叉树的镜像 【微软面试100题 第十五题】
    和为s的两个数字 【微软面试100题 第十四题】
    链表中倒数第k个结点 【微软面试100题 第十三题】
    求1+2+...+n 【微软面试100题 第十二题】
    求二叉树中结点的最大距离 【微软面试100题 第十一题】
    翻转句子中单词的顺序 【微软面试100题 第十题】
    二叉搜索树的后序遍历序列 【微软面试100题 第九题】
  • 原文地址:https://www.cnblogs.com/wang715100018066/p/6047686.html
Copyright © 2011-2022 走看看