JavaScript實現table切換的插件封裝

本文實例為大傢分享瞭JavaScript實現table切換插件的封裝代碼,供大傢參考,具體內容如下

效果圖:

HTML部分:

<div class="box">
        <div id="tabBox">
            <ul>
                <li class="liStyle">A</li>
                <li>B</li>
                <li>C</li>
            </ul>
            <ol>
                <li class="liStyle">A</li>
                <li>B</li>
                <li>C</li>
            </ol>
        </div>
</div>

css部分:

#tabBox {
            width: 600px;
            height: 450px;
            border: 3px solid #333;
        }
 
        #tabBox>ul {
            width: 100%;
            height: 50px;
            display: flex;
            justify-content: center;
            align-items: center;
        }
 
        #tabBox>ul>li {
            flex: 1;
            height: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
            color: #fff;
            background-color: skyblue;
            font-size: 30px;
        }
 
        #tabBox>ul .liStyle {
            background-color: lime;
        }
 
 
        ol {
            flex: 1;
            width: 600px;
            height: 400px;
        }
 
        ol>li {
            width: 100%;
            height: 100%;
            background-color: #ccc;
            color: #fff;
            font-size: 100px;
 
            display: none;
            justify-content: center;
            align-items: center;
        }
 
        ol>li.liStyle {
            display: flex;
        }

JavaScript構造函數部分:

function fn15() {
            function TabBox(tabbox) {
                this.tabbox = tabbox;
 
                this.tabs = tabbox.querySelectorAll("ul>li")
                this.contents = tabbox.querySelectorAll("ol>li")
            }
 
            TabBox.prototype.startSwitch = function () {
                const tb = this
 
                tb.tabs.forEach(
                    function (tab, index) {
                        tab.onclick = function (e) {
                            tb.tabs.forEach(
                                function (tab) {
                                    tab.classList.remove("liStyle")
                                }
                            )
 
                            tb.contents.forEach(
                                function (con) {
                                    con.classList.remove("liStyle")
                                }
                            )
 
                            tab.classList.add("liStyle")
 
                            tb.contents[index].classList.add("liStyle")
                        }
                    }
                )
            }
            const tabBox = document.querySelector("#tabBox")
 
            const tb = new TabBox(tabBox)
 
            tb.startSwitch()
        }
        // fn15()
 
        // 使用class實現
        function fn16() {
            class TabBox {
                constructor(tabbox) {
                    this.tabbox = tabbox;
                    this.tabs = tabbox.querySelectorAll("ul>li")
                    this.contents = tabbox.querySelectorAll("ol>li")
                }
                startSwitch() {
                    const tb = this
 
                    tb.tabs.forEach(
                        function (tab, index) {
                            tab.onclick = function (e) {
                                tb.tabs.forEach(
                                    function (tab) {
                                        tab.classList.remove("liStyle")
                                    }
                                )
 
                                tb.contents.forEach(
                                    function (con) {
                                        con.classList.remove("liStyle")
                                    }
                                )
 
                                tab.classList.add("liStyle")
 
                                tb.contents[index].classList.add("liStyle")
                            }
                        }
                    )
                }
            }
 
 
            const tabBox = document.querySelector("#tabBox")
 
            const tb = new TabBox(tabBox)
 
            tb.startSwitch()
        }
fn16()

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: