利用cookie写原生刷新后不初始化选项卡

  • HTML
 <div class="box">
        <ul>
            <li class="tit">001</li>
            <li>002</li>
            <li>003</li>
            <li>004</li>
        </ul>
        <ol>
            <li class="cont">001</li>
            <li>002</li>
            <li>003</li>
            <li>004</li>
        </ol>
    </div>
  • CSS
 * {
            margin: 0;
            padding: 0;
        }

        ul,
        ol {
            list-style: none;
        }

        .box {
            width: 400px;
            height: 300px;
            border: 2px solid skyblue;
            margin: 50px auto;
        }

        .box ul {
            width: 100%;
            height: 50px;
            display: flex;
        }

        .box ul li {
            width: 25%;
            height: 50px;
            line-height: 50px;
            text-align: center;
        }

        .box ol {
            width: 100%;
            height: 250px;
            background-color: #eee;
        }

        .box ol li {
            width: 100%;
            height: 100%;
            display: none;
            text-align: center;
            line-height: 250px;
            font-size: 25px;
            font-weight: 600;
        }

        .tit {
            background-color: #eee;
        }

        .cont {
            display: block !important;
        }
  • JavaScript
 let ul = document.querySelectorAll("div ul li");
    let ol = document.querySelectorAll("div ol li");
    const date = new Date();
    date.setDate(date.getDate() + 10);
    let index = document.cookie.slice(document.cookie.indexOf("I"), document.cookie.indexOf("b") + 3).split("=")[1];
    function Tab(num) {
        for (var i = 0; i < ul.length; i++) {
            ul[i].className = '';
            ol[i].className = '';
        }
        ul[num].className = 'tit';
        ol[num].className = 'cont';
    }
    Tab(index);
    for (var i = 0; i < ul.length; i++) {
        ul[i].index = i;
        ul[i].onclick = function () {
            Tab(this.index);
            document.cookie = `IndexTab=${this.index};expires=${date}`
        }
    }

评论吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值