如何显示单词“quot;”;评论「;如果一个数字是1和;评论“;否则使用Javascript?

如何显示单词“quot;”;评论「;如果一个数字是1和;评论“;否则使用Javascript?,javascript,jquery,facebook,if-statement,Javascript,Jquery,Facebook,If Statement,我使用的是只能访问HTML的Facebook评论。没有服务器端脚本 我的默认输出是“#注释” 如果注释数为1,如何使用Javascript使“注释”变为“注释” 谢谢你的帮助 编辑: 我使用的是标准的Facebook评论标签: 输出的HTML为: <a class="comments" href="URL"><div class="fb-comments-count" data-href="URL" fb-xfbml-state="rendered"><spa

我使用的是只能访问HTML的Facebook评论。没有服务器端脚本

我的默认输出是“#注释”


如果注释数为1,如何使用Javascript使“注释”变为“注释”

谢谢你的帮助

编辑:

我使用的是标准的Facebook评论标签:

输出的HTML为:

 <a class="comments" href="URL"><div class="fb-comments-count" data-href="URL" fb-xfbml-state="rendered"><span class="fb_comments_count">4</span></div> Comments - Click to Add Yours</a>

如果它是0,或者高于1,我只想说“评论”。如果它是1,则进行注释

function commentText(numberOfComments){
 return "comment" +((numberOfComments > 1 )?"s":"");

}

使用Facebook JS SDK查找评论数。

您可以使用以下功能:

function comment_text( num_comments ){
    return 'comment'+ ( num_comments === 1 ? '' : 's' );
}
像这样:

var comment_count = $('.fb-comments-count').text();
$(selector).text('Showing '+ comment_count + comment_text(comment_count) );

注意:我已将
num_comments>1
更改为
num_comments===1
,因为您希望显示0条注释,而不是0条注释您可以使用jQuery.length来计算comments类存在的实例数,如果数字为1,则可以使用jQuery.HTML来更改div的内容

所以

希望这有助于:

    <a class="comments" href="URL"><div class="fb-comments-count" data-href="URL">6</div> <span class="comment-text">Comments</span></a>        
    <a class="comments" href="URL"><div class="fb-comments-count" data-href="URL">0</div> <span class="comment-text">Comments</span></a>        
    <a class="comments" href="URL"><div class="fb-comments-count" data-href="URL">1</div> <span class="comment-text">Comments</span></a>        
    <a class="comments" href="URL"><div class="fb-comments-count" data-href="URL">3</div> <span class="comment-text">Comments</span></a>        
    <script type="text/javascript">
    $(document).ready(function(){
        $(".comments").each(function(){
            var count = parseInt($(this).find(".fb-comments-count").text());
            if( count > 1){
                $(this).find(".comment-text").text('Comments');
            }
            else if(count == 0){
            $(this).find(".comment-text").text('');
            }
            else{
                $(this).find(".comment-text").text('Comment');
            }
        });
    });
    </script>

$(文档).ready(函数(){
$(“.comments”)。每个(函数(){
var count=parseInt($(this).find(“.fb comments count”).text();
如果(计数>1){
$(this.find(“.comment text”).text('Comments');
}
否则如果(计数==0){
$(this.find(“.comment text”).text(“”);
}
否则{
$(this.find(“.comment text”).text('comment');
}
});
});

您存储编号的部分在哪里?当您有注释(以及有多条注释)时,html结构是什么…根据更改,计算重复的html元素并相应地生成文本。。(也许你的细节太低)顺便说一句,这是未经测试的,把它放在一起吧谢谢你的回答。我真的认为这会起作用,但它似乎显示“评论”无论什么。我试图修改你的代码,但仍然无法得到它。
var commentCount =  $('.FBcomment').length();

if(commentCount == 1){

 $('.comments').html('Comment');//would be more efficient to have an id here rather than a class

 }
    <a class="comments" href="URL"><div class="fb-comments-count" data-href="URL">6</div> <span class="comment-text">Comments</span></a>        
    <a class="comments" href="URL"><div class="fb-comments-count" data-href="URL">0</div> <span class="comment-text">Comments</span></a>        
    <a class="comments" href="URL"><div class="fb-comments-count" data-href="URL">1</div> <span class="comment-text">Comments</span></a>        
    <a class="comments" href="URL"><div class="fb-comments-count" data-href="URL">3</div> <span class="comment-text">Comments</span></a>        
    <script type="text/javascript">
    $(document).ready(function(){
        $(".comments").each(function(){
            var count = parseInt($(this).find(".fb-comments-count").text());
            if( count > 1){
                $(this).find(".comment-text").text('Comments');
            }
            else if(count == 0){
            $(this).find(".comment-text").text('');
            }
            else{
                $(this).find(".comment-text").text('Comment');
            }
        });
    });
    </script>