首页 > 其他分享 >面向对象的选项卡

面向对象的选项卡

时间:2023-03-24 19:36:07浏览次数:43  
标签:function box cont Tab 绑定 面向对象 background 选项卡

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .cont{width:400px;height:300px;border: solid 1px black;margin: 30px auto;}
        .cont ul{margin: 0;padding: 0;list-style: none;display: flex;height: 40px;line-height: 40px;text-align: center;background: #eee;border-bottom: solid 1px black;}
        .cont li{flex: 1;border-right: solid 1px black;}
        .cont li.active{background: red}

        .box p{margin: 0;height: 259px;display: none}
        .box p.active{display: block;}
        .box p:nth-child(1){background: #0a0}
        .box p:nth-child(2){background: #aa0}
        .box p:nth-child(3){background: #0aa}
    </style>
</head>
<body>
    <div class="cont">
        <ul><li class="active">1</li><li>2</li><li>3</li></ul>
        <div class="box">
            <p class="active">内容1内容1内容1内容1内容1内容1</p>
            <p>内容2</p>
            <p>内容3333333333</p>
        </div>
    </div>
</body>
<script>

    // OOA:分析
    //     选项卡:点击对应按钮的时候,切换对应的内容
    //     1.选元素
    //     2.绑定事件
    //     3.找点击的元素的索引
    //     4.根据索引,显示内容

    // OOD:设计
    // function Tab(){
    //     // 1.选元素
    //     // 2.绑定事件
    // }
    // Tab.prototype.init = function(){
    //     // 开始绑定,绑定好了之后,被触发
    //         // 3.找点击的元素的索引
    //         // 4.根据索引,显示内容
    // }
    // Tab.prototype.display = function(){
    //     // 显示呗
    // }

    // OOP:编程(填充代码)
    function Tab(){
        // 1.选元素
        this.li = document.querySelectorAll(".cont li");
        this.p = document.querySelectorAll(".cont p");
        // 2.绑定事件
        this.init();
    }
    Tab.prototype.init = function(){
        var that = this;
        // 开始绑定,绑定好了之后,被触发
        for(var i=0;i<this.li.length;i++){
            this.li[i].index = i;
            this.li[i].onclick = function(){
                // 3.找点击的元素的索引
                that.abc = this.index;//被点击的那个索引
                // 4.根据索引,显示内容
                that.display();
            }
        }
    }
    Tab.prototype.display = function(){
        // 显示呗
        for(var i=0;i<this.li.length;i++){
            this.li[i].className = "";
            this.p[i].className = "";
        }
        this.li[this.abc].className = "active";
        this.p[this.abc].className = "active";
    }

    new Tab();

</script>
</html>

 

长风破浪会有时,直挂云帆济沧海



标签:function,box,cont,Tab,绑定,面向对象,background,选项卡
From: https://blog.51cto.com/u_15961676/6147973

相关文章

  • 面向对象的概念
    //原料functionfn(){//将这个函数中的this改变,指向new新创建的对象//加工this.name="root";}////出厂varf=new......
  • 面向对象-拖拽
    <script>//一个页面上实现两个拖拽//不同的效果:一个有边界限定,一个没有functionDrag(ele){this.ele=ele;//因为使用事件监听式......
  • JavaScript 面向对象
    类和对象类是用于定义对象的模板或蓝图;它包含对象的属性和方法,我们可以使用class关键字来定义类。classPerson{constructor(name,age){this.name=name;t......
  • java面向对象
    封装的优点1.良好的封装能够减少耦合。2.类内部的结构可以自由修改。3.可以对成员变量进行更精确的控制。4.隐藏信息,实现细节。继承的特性子类拥有......
  • 面向对象
    面向对象1.初识面向对象面向过程&面向对象属性+方法=类对于描述复杂的事物,为了从宏观上把握,从整体上合理分析,我们需要使用面向对象的思路来分析整个系统,但是具体到围观......
  • pathlib -- 面向对象的文件系统路径
    ......
  • 面向对象
    面向对象面向对象编程(Object-OrientedProgramming,OOP)面向对象编程的本质就是:以类的方式组织代码,以对象的组织封装数据对于描述复杂的事物,为了从宏观上把握、从整体上合......
  • fastadmin生成Tab选项卡
    fastadmin生成Tab选项卡2022-12-23不要看什么狗屁文档,都是骗人的,让我踩了半天坑。  关于fastadmin生成选项卡,最简单,最直接的办法就是【字段命名】,不要添加任何命......
  • Python 面向对象
    Python面向对象目录Python面向对象1编程范式介绍1.1面向过程编程(ProceduralProgramming)1.2面向对象编程2面向对象编程(Object-OrientedProgramming)2.1Class......
  • 面向对象全部内容
    目录面向过程面向对象类的定义和对象的产生定制对象自己独有的属性属性的查找顺序绑定方法和非绑定方法隐藏属性property装饰器继承super和mro多态组合内置方法(魔术方法简......