js部分
//评论点赞
$(".comment_content").on("click","dl .com_t,dl dd .re_t",function(e){ //点击动态加载的评论点赞和回复评论点赞
e.stopPropagation();
var th=$(e.currentTarget).find(".thumbs-up");
//var postid=$(e.currentTarget).closest("dl").attr("postid"); //要确保点击**评论点赞**和**回复评论点赞**都能取到postid,所以用下面这一句
var postid=$(e.currentTarget).closest(".postid").attr("postid");
if(api.pageParam.fromName=='root'){
var url = 'http://' + serv_ip + '/app/index.php?method52=b.article.likepost';
var data = {
topicid:topicid ,
postid:postid //传入该篇文章的id 和评论的id (评论和回复评论id都为不同 postid)
};
if(myid) {
msAjax(url, data, function (result) {
if (result.success) {
//提供接口值为1是点赞过的,接口值为0是未点赞的
if (result.data == '1') {
th.attr("src","images/community/thumbs-uped.png");
var num=Number( $(e.currentTarget).find("span").text())+1;
$(e.currentTarget).find("span").text(num);
// $(e.currentTarget).find("span").text(Number( th.text())+1);
} else {
th.attr("src","images/community/thumbs-up.png");
var num=Number( $(e.currentTarget).find("span").text())-1;
$(e.currentTarget).find("span").text(num);
}
} else {
showExMsg(result.reason);
}
})
}else {
gotoLogin();
}
}
});
动态加载html部分
+ ' <div class="thumbs-up '+(item.liked=='1'?'active':'')+'"><div>' + item.likecount + '</div></div>'
css部分
.thumbs-up.active{
background-image: url("images/community/thumbs-uped.png");
}