ES6 class 继承 与面向对象封装开发简单实例

<!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>选项卡</title>
    <style>
        .active {
            background-color: #789218;
            color: aqua;
        }

        .box div {
            height: 200px;
             200px;
            border: 1px solid #cccccc;
            display: none;
        }
    </style>
</head>

<body>
    <script>
        class Tab {
            constructor(id) {
                this.oBox = document.getElementById(id);
                this.aBtn = this.oBox.getElementsByTagName('input');
                this.aDiv = this.oBox.getElementsByTagName('div');
                this.iNow = 0;
                this.init();
            }
            init() {
                for (let i = 0; i < this.aBtn.length; i++) {
                    this.aBtn[i].onclick = () => {
                        this.iNow = i;
                        document.title = this.iNow;
                        this.hide();
                        this.show(i);
                    }
                }
            }
            hide() {
                for (var i = 0; i < this.aDiv.length; i++) {
                    this.aDiv[i].style.display = 'none';
                    this.aBtn[i].className = '';
                }
            }
            show(index) {
                this.aDiv[index].style.display = 'block';
                this.aBtn[index].className = 'active';
            }
        }
        class AuToTab extends Tab {
            constructor(id) {
                super(id);
                setInterval(this.next.bind(this), 1000)
            }
            next() {
                this.iNow++;
                if (this.iNow == this.aBtn.length) this.iNow = 0;
                this.hide();
                this.show(this.iNow);
            }
        }
        window.onload = () => {
            new Tab('box');
            new AuToTab('box2');
        }
    </script>
    <div id="box" class="box">
        <input type="button" value="aaa" class="active">
        <input type="button" value="bbb">
        <input type="button" value="ccc">
        <div style="display: block;">1111</div>
        <div>2222</div>
        <div>3333</div>
    </div>
    <div id="box2" class="box">
        <input type="button" value="aaa" class="active">
        <input type="button" value="bbb">
        <input type="button" value="ccc">
        <div style="display: block;">1111</div>
        <div>2222</div>
        <div>3333</div>
    </div>
</body>

</html>
原文地址:https://www.cnblogs.com/dekui/p/9008565.html