javascript
  • jquery
  • 2014-10-29 2 views 0 likes 
    0

    У меня есть этот код ниже:Append Количество вара после HTML (сердце вара) - 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; 
    

    сердце - это умение, которое можно щелкнуть, и я не хочу, чтобы граф был доступен для клика.

    весь код:

    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

    +0

    @ user3266957 если вы попробовали второй метод, в нем было несколько проблем ... теперь обновлено ... также см. [Первый метод] (http://jsfiddle.net/arunpjohny/chobju1r/3/) и [второй метод ] (http://jsfiddle.net/arunpjohny/chobju1r/6/) –

    +0

    yup спасибо. – user3266957

    Смежные вопросы