ES6寫tab切換

<!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>
</head>

樣式

<style>

    .active {
        color: orange;
    }

    .parent {
        border: 2px solid #000;
        width: 200px;
        height: 200px;
        background: -webkit-radial-gradient(rgb(144, 132, 144), rgb(14, 172, 225), rgb(16, 243, 164));
        border-radius: 5px;
    }

    .parent span {
        height: 30px;
        line-height: 30px;
        padding: 12px;
        border-bottom: 2px solid #000;
    }

    .parent div {
        margin-top: 50px;
        text-align: center;
        color: wheat;
    }

</style>

<body>

div模塊

    <div class="parent">
        <span class="title">title1</span>
        <span class="title">title2</span>
        <span class="title">title3</span>
        <div class="content">content1</div>
        <div class="content">content2</div>
        <div class="content">content3</div>
    </div>

script代碼

<script>
       class Tabs {
           constructor(el) {
               this.el = document.querySelector(el);
               this.init();// 初始化
               this.bindEvent();// 綁定事件
           }
           init() {
               // 獲取titles
               this.titles = this.el.querySelectorAll(".title");
               // 獲取contents
               this.contents = this.el.querySelectorAll(".content");
               // 默認(rèn)所有的div
               for (let i = 0; i < this.contents.length; i++) {
                   this.contents[i].style.display = "none";
               }
               // 顯示第一個(gè)content
               this.contents[0].style.display = "block";
               // 給第一個(gè)title添加active類
               this.titles[0].classList.add("active");
               // 記錄當(dāng)前是第幾個(gè)顯示的
               this.currentIndex = 0;
           }
           bindEvent() {
               for (let i = 0; i < this.titles.length; i++) {
                   this.titles[i].addEventListener("click", () => {
                       this.titleHd(i);
                   }, false);
               }
           }
           titleHd(index) {
               console.log(index, "被單擊了");
               console.log("把以前顯示的div隱藏");
               console.log("顯示" + index + "div");
               console.log("把以前active去掉");
               console.log("給" + index + "title加active");
               console.log("設(shè)置當(dāng)前currentIndex 為" + index);
               this.titles[this.currentIndex].classList.remove("active");
               this.contents[this.currentIndex].style.display = "none";
               this.titles[index].classList.add("active");
               this.contents[index].style.display = "block";
               this.currentIndex = index;
           }
       }
       var tab = new Tabs(".parent");
</script>

</body>

</html>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概要 64學(xué)時(shí) 3.5學(xué)分 章節(jié)安排 電子商務(wù)網(wǎng)站概況 HTML5+CSS3 JavaScript Node 電子...
    阿啊阿吖丁閱讀 9,793評(píng)論 0 3
  • 前端開發(fā)面試題 面試題目: 根據(jù)你的等級(jí)和職位的變化,入門級(jí)到專家級(jí),廣度和深度都會(huì)有所增加。 題目類型: 理論知...
    怡寶丶閱讀 2,670評(píng)論 0 7
  • 概述 document節(jié)點(diǎn)是文檔的根節(jié)點(diǎn),每張網(wǎng)頁(yè)都有自己的document節(jié)點(diǎn)。window.document屬...
    許先生__閱讀 725評(píng)論 0 2
  • 請(qǐng)參看我github中的wiki,不定期更新。https://github.com/ivonzhang/Front...
    zhangivon閱讀 7,743評(píng)論 2 19
  • 一、概述 document節(jié)點(diǎn)是整個(gè)文檔樹的頂層節(jié)點(diǎn),每張網(wǎng)頁(yè)都有自己的document節(jié)點(diǎn)。window.doc...
    周花花啊閱讀 1,417評(píng)論 0 1

友情鏈接更多精彩內(nèi)容