2014-10-29 75 views
0

我有下面这段代码:追加数VAR HTML(心脏VAR) - jQuery的

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

我想heart.html外计数元素()。像这样:

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

心是一个点击能够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; 
} 

任何帮助,将不胜感激。

回答

1

您需要使用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); 
+0

感谢您的帮助。我只是试过这个。计数没有显示。我再次检查我的代码以查看导致问题的原因。 – user3266957 2014-10-29 06:50:05

+0

@ user3266957如果您尝试过第二种方法,那么它中几乎没有问题...现在更新...也参见[第一种方法](http://jsfiddle.net/arunpjohny/chobju1r/3/)和[第二种方法](http://jsfiddle.net/arunpjohny/chobju1r/6/) – 2014-10-29 06:59:50

+0

哟感谢得到它。 – user3266957 2014-10-29 08:01:53