我开发了一个简单的like/unlike 脚本。
喜欢脚本
function like(blog_id,object_id,object_type,user_id,default_count)
{
if( user_id == 0 ) {
jQuery("#show_login_box").fancybox({cyclic: true}).trigger('click');
} else {
if( default_count == 0 ) {
var new_count = '1';
var link = 'unlike(\'' + blog_id + '\',\'' + object_id + '\',\'' + object_type + '\',\'' + user_id + '\',\'' + new_count + '\')\;';
jQuery('.likes').html('You like this.');
} else {
var new_count = parseInt(default_count) + 1;
if ( jQuery('.like-user').length ) {
var name = jQuery('.like-user').html();
var link = 'unlike(\'' + blog_id + '\',\'' + object_id + '\',\'' + object_type + '\',\'' + user_id + '\',\'' + new_count + '\')\;';
jQuery('.likes').html('You and <span class="like-user">' + name + '</span> likes this.');
} else {
var link = 'unlike(\'' + blog_id + '\',\'' + object_id + '\',\'' + object_type + '\',\'' + user_id + '\',\'' + new_count + '\')\;';
jQuery('.likes').html('You and <span class="like-user"><a href="#">' + default_count + ' others</a></span> like this.');
}
}
jQuery('.like_click').attr('onclick',link);
jQuery('.like_click span').html('<img src="/wp-content/plugins/assets/images/icons/unlike-icon.png"> Unlike');
jQuery.ajax({
url: '/wp-content/plugins/assets/like.php',
type: 'POST',
data: { object_id: object_id, user_id: user_id, type: 'like', blog_id: blog_id, object_type: object_type, count: default_count },
dataType: 'json',
success: function(data)
{
// jQuery('#' + object_id + '_count').html(data.total);
}
});
}
}
不同于脚本
function unlike(blog_id,object_id,object_type,user_id,default_count)
{
if( default_count == 1 ) {
var not_like = '0';
var link = 'like(\'' + blog_id + '\',\'' + object_id + '\',\'' + object_type + '\',\'' + user_id + '\',\'' + default_count + '\',\'' + not_like + '\')\;';
jQuery('.likes').html('');
} else {
var new_count = parseInt(default_count) - 1;
if ( jQuery('.like-user').length && default_count > 1 ) {
var name = jQuery('.like-user').html();
var link = 'like(\'' + blog_id + '\',\'' + object_id + '\',\'' + object_type + '\',\'' + user_id + '\',\'' + new_count + '\')\;';
jQuery('.likes').html('<span class="like-user">' + name + '</span> like this.');
} else {
var link = 'like(\'' + blog_id + '\',\'' + object_id + '\',\'' + object_type + '\',\'' + user_id + '\',\'' + new_count + '\')\;';
jQuery('.likes').html('<span class="like-user"><a href="#">' + new_count + ' people</a></span> like this.');
}
}
jQuery('.like_click').attr('onclick',link);
jQuery('.like_click span').html('<img src="/wp-content/plugins/assets/images/icons/like-icon.png"> Like');
jQuery.ajax({
url: '/wp-content/plugins/assets/like.php',
type: 'POST',
data: { object_id: object_id, user_id: user_id, type: 'unlike', blog_id: blog_id, object_type: object_type, count: default_count },
dataType: 'json',
success: function(data)
{
// jQuery('#' + object_id + '_count').html(data.total);
}
});
}
HTML
<a onclick="like('85','1','product','1','0');" class="button like_click">
<span><img src="wp-content/plugins/assets/images/icons/like-icon.png"> Like</span>
</a><div class="likes"></div>
场景:我喜欢一个帖子,我是第一个,所以返回的消息是“你喜欢这个”。并且没有刷新我不喜欢它,它现在删除了“你喜欢这个”消息。这是正确的反应。
问题:没有刷新同一个帖子,我再次喜欢它,现在消息显示“你和其他 1 个人喜欢这个”而不是“你喜欢这个”。信息。
JS 小提琴演示:http: //jsfiddle.net/z8mKX/2/
我无法配置我的脚本出了什么问题。请帮忙