Javascript html后追加计数变量(heart-var)-jQuery
我有以下代码:Javascript html后追加计数变量(heart-var)-jQuery,javascript,jquery,Javascript,Jquery,我有以下代码: heart.html("<i class='glyphicon glyphicon-remove-sign'></i>   "+count); heart.html(“ ”+count); 我希望count元素位于heart.html()之外。比如: heart.html("<i class='glyphicon glyphicon-remove-sign'></i>&a
heart.html("<i class='glyphicon glyphicon-remove-sign'></i>   "+count);
heart.html(“ ”+count);
我希望count元素位于heart.html()之外。比如:
heart.html("<i class='glyphicon glyphicon-remove-sign'></i>   ")+count;
heart.html(“ ”)+count;
heart是一个可点击的div,我不希望计数是可点击的
整个代码:
jQuery(document).ready(function()
{
jQuery('body').on('click','.jm-post-like',function(event)
{
event.preventDefault();
heart = jQuery(this);
post_id = heart.data("post_id");
heart.html("<i class='glyphicon glyphicon-cog'></i>");
jQuery.ajax
({
type: "post",
url: ajax_var.url,
data: "action=jm-post-like&nonce="+ajax_var.nonce+"&jm_post_like=&post_id="+post_id,
success: function(count)
{
if( count.indexOf( "already" ) !== -1 )
{
var lecount = count.replace("already","");
if (lecount === "0")
{
lecount = "0";
}
heart.prop('title', 'Like');
heart.removeClass("liked");
heart.html("<i class='glyphicon glyphicon-heart'></i>   "+lecount);
}
else
{
heart.prop('title', 'Unlike');
heart.addClass("liked");
heart.html("<i class='glyphicon glyphicon-remove-sign'></i>   "+count);
}
}
});
});
});
function getPostLikeLink( $post_id )
{
$like_count = get_post_meta( $post_id, "_post_like_count", true ); // Get post likes.
$count = ( empty( $like_count ) || $like_count == "0" ) ? '0' : esc_attr( $like_count );
if ( AlreadyLiked( $post_id ) )
{
$class = esc_attr( ' liked' );
$title = esc_attr( 'Unlike' );
$heart = '<i class="glyphicon glyphicon-remove-sign"></i>   ';
}
else
{
$class = esc_attr( '' );
$title = esc_attr( 'Like' );
$heart = '<i class="glyphicon glyphicon-heart"></i>   ';
}
$output = '<a href="#" class="jm-post-like'.$class.'" data-post_id="'.$post_id.'" title="'.$title.'">'.$heart.' </a>'.$count;
return $output;
}
jQuery(文档).ready(函数()
{
jQuery('body')。on('click','jm post-like',函数(事件)
{
event.preventDefault();
heart=jQuery(this);
post_id=heart.data(“post_id”);
html(“”);
jQuery.ajax
({
类型:“post”,
url:ajax_var.url,
数据:“action=jm post like&nonce=“+ajax_var.nonce+”&jm_post_like=&post_id=“+post_id,
成功:功能(计数)
{
if(count.indexOf(“已”)!=-1)
{
var-lecount=count.replace(“已经”和“);
如果(lecount==“0”)
{
lecount=“0”;
}
heart.prop('title','Like');
heart.removeClass(“喜欢”);
html(“+lecount”);
}
其他的
{
heart.prop(‘标题’、‘不一样’);
addClass(“喜欢”);
html(“+count”);
}
}
});
});
});
函数getPostLikeLink($post\u id)
{
$like_count=get_post_meta($post_id,“_post_like_count”,true);//获取喜欢的帖子。
$count=(空($like_count)| |$like_count==“0”)?“0”:esc_attr($like_count);
如果(已初始化($post_id))
{
$class=esc_attr('liked');
$title=esc_attr(‘不一样’);
$heart=';
}
其他的
{
$class=esc_attr(“”);
$title=esc_attr('Like');
$heart=';
}
$output=''.$count;
返回$output;
}
任何帮助都将不胜感激。您需要使用
heart.html(“  ”)
心。后(数);
但是如果您多次调用脚本,则数字将一直处于前置状态,而不是被覆盖。。。所以
heart.html("<i class='glyphicon glyphicon-remove-sign'></i>   ");
var $counter = heart.next('.counter');
if (!$counter.length) {
$counter = $('<span />', {
'class': 'counter'
}).insertAfter(heart);
}
$counter.html(count);
heart.html(“ ”);
var$counter=heart.next('.counter');
如果(!$counter.length){
$计数器=$(''{
“类”:“计数器”
}).插入者(心脏);
}
$counter.html(count);
谢谢您的帮助。我刚试过这个。伯爵没有露面。我正在再次检查我的代码,看看是什么导致了问题。@user3266957如果您尝试了第二种方法,那么其中几乎没有问题。。。现在更新。。。也看到和
heart.html("<i class='glyphicon glyphicon-remove-sign'></i>   ");
var $counter = heart.next('.counter');
if (!$counter.length) {
$counter = $('<span />', {
'class': 'counter'
}).insertAfter(heart);
}
$counter.html(count);