点赞效果

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .out{width: 500px;border: 1px solid #999;margin: 50px auto;padding: 20px;}
        .out .row{border-bottom: 1px solid #ccc;}
        .out .btn{cursor: pointer;display: inline-block;}/*手*/
        .out .btn i{display: inline-block;width: 20px;height: 20px;background: url("img/zan.png");
            margin-right: 6px;vertical-align: middle;}
        .out .btn.on i{background-image: url("img/zan-h.png");}
        .out .btn b{vertical-align: middle;}
    </style>
    <script type="text/javascript" src="js/jquery.js"></script>
</head>
<body>
<div class="out">
    <div class="row">
        <h2>朋友圈标题</h2>
        <p>内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容</p>
        <div class="btn" data-key="on">
            <i></i><b>5</b>
        </div>
    </div>
    <div class="row">
        <h2>朋友圈标题</h2>
        <p>内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容</p>
        <div class="btn" data-key="on">
            <i></i><b>10</b>
        </div>
    </div>
    <div class="row">
        <h2>朋友圈标题</h2>
        <p>内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容</p>
        <div class="btn" data-key="on">
            <i></i><b>15</b>
        </div>
    </div>
    <div class="row">
        <h2>朋友圈标题</h2>
        <p>内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容</p>
        <div class="btn" data-key="on">
            <i></i><b>20</b>
        </div>
    </div>
    <div class="row">
        <h2>朋友圈标题</h2>
        <p>内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容</p>
        <div class="btn" data-key="on">
            <i></i><b>25</b>
        </div>
    </div>
</div>
<script>
    $(function () {
        $('.btn').click(function () {
            if ($(this).attr('data-key')=='on'){
                //点赞
                $(this).addClass('on');
                var num=Number($(this).find('b').html());//找到点赞数目,但是却是字符串类型,用number将字符串类型转换为数字类型
                alert(typeof num);//typeof看类型
                alert(num+1);//在点赞数目基础上+1
                $(this).find('b').html(num+1);

                $(this).attr('data-key','');//点击>1次时候将无法继续增加赞数,让他等于空
            }else {
                //取消赞
                $(this).removeClass('on');
                var num=Number($(this).find('b').html());//找到点赞数目,但是却是字符串类型,用number将字符串类型转换为数字类型
                alert(typeof num);//typeof看类型
                alert(num-1);//在点赞数目基础上-1
                $(this).find('b').html(num-1);

                $(this).attr('data-key','on');//点击<1次时候将无法继续取消赞数,让他等于on
            }

        })
    })
</script>
</body>
</html>

效果图:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值