gpt4 book ai didi

javascript - 在 html 之后追加计数 var (heart var) - jQuery

转载 作者:行者123 更新时间:2023-12-03 11:33:45 25 4
gpt4 key购买 nike

我有下面的代码:

heart.html("<i class='glyphicon glyphicon-remove-sign'></i>&nbsp&nbsp&nbsp;"+count);

我想要 heart.html() 之外的 count 元素。像这样:

heart.html("<i class='glyphicon glyphicon-remove-sign'></i>&nbsp&nbsp&nbsp;")+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>&nbsp&nbsp&nbsp;"+lecount);
}
else
{
heart.prop('title', 'Unlike');
heart.addClass("liked");
heart.html("<i class='glyphicon glyphicon-remove-sign'></i>&nbsp&nbsp&nbsp;"+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>&nbsp&nbsp&nbsp;';
}
else
{
$class = esc_attr( '' );
$title = esc_attr( 'Like' );
$heart = '<i class="glyphicon glyphicon-heart"></i>&nbsp&nbsp&nbsp;';
}
$output = '<a href="#" class="jm-post-like'.$class.'" data-post_id="'.$post_id.'" title="'.$title.'">'.$heart.'&nbsp;</a>'.$count;
return $output;
}

如有任何帮助,我们将不胜感激。

最佳答案

您需要使用after()

heart.html("<i class='glyphicon glyphicon-remove-sign'></i>&nbsp&nbsp&nbsp;")
heart.after(count);

但是如果您多次调用脚本,数字将继续保留在前面,而不是被覆盖......所以

heart.html("<i class='glyphicon glyphicon-remove-sign'></i>&nbsp&nbsp&nbsp;");
var $counter = heart.next('.counter');
if (!$counter.length) {
$counter = $('<span />', {
'class': 'counter'
}).insertAfter(heart);
}
$counter.html(count);

关于javascript - 在 html 之后追加计数 var (heart var) - jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26624101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com