123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <link rel="stylesheet" href="https://at.alicdn.com/t/c/font_4974983_z46pr7nyg5p.css">
- <style>
- body{
- background-color: #f9f9f9;
- }
- .like-box{
- width: 200px;
- height: 200px;
- border-color: #fff;
- box-shadow: 0 0 10px rgba(0,0,0,0.5);
- position: fixed;
- top:50%;
- left: 50%;
- margin-top: -160px;
- margin-left: -140px;
- padding: 60px 40px;
- text-align: center;
- }
- .like-box .like-title{
- font-size: 20px;
- font-weight: bold;
- margin-bottom: 20px;
- }
- .like-box .like-icon .iconfont{
- font-size: 70px;
- color: #aaa;
- }
- .like-box .like-icon .iconfont:hover{
- color: red;
- cursor: pointer;
- }
- .like-box .like-num{
- font-size: 30px;
- font-weight: bold;
- }
- .like-box .like-text{
- margin-top: 10px;
- font-size: 12px;
- color: #aaa;
- }
- </style>
- </head>
- <body>
- <div class="like-box">
- <div class="like-title">点赞计数器</div>
- <div class="like-icon">
- <i class="iconfont icon-icons-"></i>
- </div>
- <div class="like-num"></div>
- <div class="like-text">点击心形图标进行点赞</div>
- </div>
- <script>
- // 功能一 实现向标签内插入数组
- // 获取数字标签 返回类数组
- var likeIcon = document.getElementsByClassName("like-num");
- // 从类数组中取出数字标签
- likeIcon = likeIcon[0];
- // 向数字标签中写入内容
- likeIcon.innerText = 0;
- // 功能二实现累加
- // 获取心形图标
- var likeBtn = document.getElementsByClassName("iconfont");
- // 从数组中取出心形图标
- likeBtn = likeBtn[0];
- // 给心形图标添加点击事件
- // 定义变量用作于累计的数字
- var num = 0;
- likeBtn.onclick = function(){
- // 让num进行一个累加操作
- num++;
- // 把累加后的数字赋值给数字标签
- likeIcon.innerText = num;
- }
- </script>
- </body>
- </html>
|