简单年历

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>万年历</title>

    <style>

        .box{

            width: 330px;

            height: 400px;

            border: 1px solid black;

            margin: 0 auto;

        }

        ul li{

            list-style: none;

            display: inline-block;

            border: 1px solid black;

            width: 120px;

            height: 80px;

            color: white;

            text-align: center;

            margin-bottom: 40px;

            margin-right: 10px;

            background: black;

        }

        ul li:hover{

            background: pink;

            color: white;

 

        }

        ul li h2{

            margin-top: 5px;

        }

        .secshow{

            margin: 0 auto;

            width: 90%;

            height: 100px;

            border: 1px solid black;

            text-align: center;

        }

    </style>

</head>

<body>

    <section class="box">

        <ul id="us">

            <li>

                <h2>春天</h2>

                <p>万物复苏</p>

            </li>

            <li>

                <h2>夏天</h2>

                <p>炎热</p>

            </li>

            <li>

                <h2>秋天</h2>

                <p>干燥</p>

            </li>

            <li>

                <h2>冬天</h2>

                <p>寒冷</p>

            </li>

        </ul>

        <section class="secshow" id="secshow">

            

        </section>

    </section>

</body>

</html>

<script>

    //获取ul

    let us=document.getElementById('us')

    //获取底下显示盒子

    let secshow=document.getElementById('secshow')

    //获取ul底下的所有li

    let lis=us.getElementsByTagName('li')

 

    let j=['春天','夏天','秋天','冬天']

    let des=['万物复苏','炎热','干燥','寒冷']

    //遍历所有li

    for(let i=0;i<lis.length;i++){

        //给li添加动态索引

        lis[i].index=i

        //给每个li添加点击事件

        lis[i].οnclick=function () {

            //清空secshow

            secshow.innerHTML=''

            //创建一个h2

            let h2=document.createElement('h2');

            //当前选中的li的索引值等于数组对应的值

            h2.innerText=j[this.index]

            //创建一个p标签

            let p=document.createElement('p')

            //当前选中索引的值等于对象数组的值

            p.innerText=des[this.index]

            //追加进显示卡里

            secshow.appendChild(h2)

            secshow.appendChild(p)

          }

    }

</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值