简体   繁体   中英

Using jQuery to have facebook like count

Since opengraph only provide those informations

array(2) { ["engagement"]=> array(4) { 
["reaction_count"]=> int(1) 
["comment_count"]=> int(0) 
["share_count"]=> int(1) 
["comment_plugin_count"]=> int(0)  
}

I'm trying to have the like count of a url.

So I found this information on the button like, which have this number.

<span class="_49vh _2pi7">J’aime</span><span class="_5n6h _2pih" id="u_0_2_/+">2</span>

I found that the span with the count has an Id but it change for each page. But the class used is the same for each pages ._5n6h ._2pih

I'm trying to have the count of this span.

My script was very simple

  <script type="text/javascript" async defer>
                                            $(document).ready(function () {
                                                var elmId = $('._5n6h._2pih').html();
                                                console.log(elmId);
                                            })
                                        </script>

I can not have them

Uncaught TypeError: $(...).html is not a function

I don't know what am I doing wrong.

You first need to include jQuery at the top, preferably in the <head> then try the following:

    $(document).ready(function(){
   var count = $('span._5n6h._2pih');
   var like_count = parseInt(count.text());

   console.log(like_count); //outputs 2



    //I have played a bit with your code adding a click listener to the span. Each time you click, it increments by 1
        like.click(function(){
like_count += 1;
count.text(like_count);
            });
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM