<!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>
效果图: