js页面切换

<style>
    * {
        margin: 0;
        padding: 0;
    }

    #title {
        overflow: hidden;
        margin: 100px auto;
        width: 900px;
        list-style: none;
    }

    #title li {
        float: left;
        width: 300px;
        height: 50px;
        text-align: center;
        line-height: 50px;
        font-size: 30px;
        background-color: blue;
    }

    /* #title li:first-child {
        background-color: gold;
    } */

    #contents {
        list-style: none;
        width: 900px;
        margin: 50px auto;
    }

    #contents li {
        width: 900px;
        height: 500px;
        text-align: center;
        line-height: 500px;
        font-size: 50px;
    }

    #contents li:first-child {
        background-color: aqua;
    }

    #contents li:nth-child(2) {
        background-color: aquamarine;
        display: none;
    }

    #contents li:last-child {
        background-color: antiquewhite;
        display: none;
    }
</style>
<body>
    <div>
        <ul id="title">
            <li>标题一</li>
            <li>标题二</li>
            <li>标题三</li>
        </ul>
    </div>
    <div>
        <ul id="contents">
            <li>内容一</li>
            <li>内容二</li>
            <li>内容三</li>
        </ul>
    </div>
<script>
    var title = document.getElementById('title').getElementsByTagName('li')
    var contents = document.getElementById('contents').getElementsByTagName('li')
    for (let i = 0; i < title.length; i++) {
        title[i].onclick = function () {
            for (let j = 0; j < contents.length; j++) {
                if (i == j) {
                    this.style.backgroundColor = 'gold';
                    contents[j].style.display = "block"
                } else {
                    title[j].style.backgroundColor = 'blue';
                    contents[j].style.display = 'none';
                }
            }
        }
    }
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值