2014-10-29 71 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