簡単なタブバー切り替え

22422 ワード

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>Documenttitle>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        li {
            list-style-type: none;
        }
        
        .tab {
            width: 978px;
            margin: 100px auto;
        }
        
        .tab_list {
            height: 39px;
            border: 1px solid #ccc;
            background-color: #f1f1f1;
        }
        
        .tab_list li {
            float: left;
            height: 39px;
            line-height: 39px;
            padding: 0 20px;
            text-align: center;
            cursor: pointer;
        }
        
        .tab_list .current {
            background-color: #c81623;
            color: #fff;
        }
        
        .item_info {
            padding: 20px 0 0 20px;
        }
        
        .item {
            display: none;
        }
    style>
head>

<body>
    <div class="tab">
        <div class="tab_list">
            <ul>
                <li class="current"> li>
                <li> li>
                <li> li>
                <li> (50000)li>
                <li> li>
            ul>
        div>
        <div class="tab_con">
            <div class="item" style="display: block;">
                 
            div>
            <div class="item">
                 
            div>
            <div class="item">
                 
            div>
            <div class="item">
                 (50000) 
            div>
            <div class="item">
                 
            div>

        div>
    div>
    <script type="text/javascript">
        // 
        var tab_list = document.querySelector(".tab_list");
        var lis = tab_list.querySelectorAll("li");
        var items = document.querySelectorAll(".item");
        
        // tab 
        for(var i=0; i<lis.length; i++){
            //  
           lis[i].setAttribute("data-index",i);
           // 
           lis[i].onclick = function() {
               // tab class , 
               for(var i=0; i<lis.length; i++){
                   lis[i].className = "";
               }
               this.className = "current";
               
               //  
               var index = this.getAttribute("data-index");
               for(var i=0; i<items.length; i++){
                    items[i].style.display = "none";
               }
               items[index].style.display = "block";
           }
        }
    script>
body>
html>