jquery 如何合并JSON代码显示评论号

iqjalb3h  于 2023-08-04  发布在  jQuery
关注(0)|答案(1)|浏览(98)

最近,我为我的博客或blogspot网站创建了一个简单的代码来显示作者的数据,如作者的名字与URL,图像和他们发表的文章编号。
然后我试着显示作者的评论号。通过这个网址https://tailwindbt.blogspot.com/feeds/comments/default?alt=json-in-script&max-results=500&callback=handleJsonpData,我们可以得到一个博客网站的所有评论。在这里,我们可以得到所有的评论与名称,评论消息和许多。
因此,我试图,显示作者的评论数旁边他们的名字.我写了这个代码,但它不工作。在我的博客里有三个作者,他们中的任何一个都有一些评论。
我的代码有两个扇区。我想使这两个代码的组合,以显示评论作者的数量,如果作者有任何意见。

这是第一部分,以获得评论。在这里我手动应用了一个名称,并检查该名称是否有注解,并显示编号。

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<div id="comment-count"></div>
<div id="iacman-comment-count"></div>

<script>
  function displayCommentCount(data) {
    var totalCommentCount = 0;
    var iacmanCommentCount = 0;
    if ('entry' in data.feed) {
      totalCommentCount = data.feed.entry.length;
      for (var i = 0; i < data.feed.entry.length; i++) {
        var authorName = data.feed.entry[i].author[0].name.$t;
        if (authorName === 'Iacman') {
          iacmanCommentCount++;
        }
      }
    }
    document.getElementById('comment-count').innerHTML = '<h2>Total Comments: ' + totalCommentCount + '</h2>';
    document.getElementById('iacman-comment-count').innerHTML = '<h2>Comments by Iacman: ' + iacmanCommentCount + '</h2>';
  }
  function handleJsonpData(data) {
    displayCommentCount(data);
  }
  var script = document.createElement('script');
  script.src = 'https://tailwindbt.blogspot.com/feeds/comments/default?alt=json-in-script&max-results=500&callback=handleJsonpData';
  document.body.appendChild(script);
</script>

字符串

这是获取博客作者详细信息的第二部分。

<link href='https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.19/tailwind.min.css' rel='stylesheet'/>
<script crossorigin='anonymous' src='https://kit.fontawesome.com/7dfc182d96.js'></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<style>.author-image {cursor: pointer;}</style>

<div class="mb-0 mt-12">
  <h4 class="mb-0 text-black dark:text-gray-300"><i class="fa-solid fa-user-vneck-hair"></i> Authors and Writers </h4>
</div>
<div class="tbt_all_authors-list mt-0 mb-16 grid grid-cols-1 sm:grid-cols-1 md:grid-cols-3 lg:grid-cols-3 xl:grid-cols-3 gap-5"></div>

<script>
  let feedURL = "https://tailwindbt.blogspot.com/feeds/posts/default?alt=json-in-script&callback=?&max-results=500";
  $.getJSON(feedURL, function(data) {
    let authors = [];
    $.each(data.feed.entry, function(index, entry) {
      if (entry.author) {
        let authorName = entry.author[0].name.$t;
        let authorImage = entry.author[0].gd$image.src;
        let authorAbout = '';
        if (entry.author[0].gd$about) {
          authorAbout = entry.author[0].gd$about.$t;
        }
        let authorProfileUrl = entry.author[0].uri.$t; // Extract the author profile URL
        let authorExists = false;
        let authorIndex;
        $.each(authors, function(index, author) {
          if (author.name === authorName) {
            authorExists = true;
            authorIndex = index;
          }
        });
        if (authorExists) {
          authors[authorIndex].count++;
        } else {
          authors.push({ name: authorName, image: authorImage, count: 1, about: authorAbout, profileUrl: authorProfileUrl });
        }
      }
    });
    authors.sort(function(a, b) {
      return b.count - a.count;
    });
    $.each(authors, function(index, author) {
      let html = '<div class="flex bg-white dark:bg-gray-700 dark:text-gray-300 shadow rounded">' +
        '<a href="' + author.profileUrl + '" target="_blank" class="author-image flex items-start px-3 py-3">' +
        '<div class="w-20 h-20 rounded-full object-cover mr-4 shadow" style="background-image: url(' + author.image + '); background-repeat: no-repeat; background-position: center; background-size: cover;"></div>' +
        '<div class="">' +
        '<div class="flex items-center justify-between">' +
        '<div class="text-md font-semibold text-gray-900 dark:text-gray-300 mt-3">' + author.name + '</div>' +
        '</div>' +
        '<div class="text-gray-700 dark:text-gray-400">Posts: ' + author.count + ' </div>' +
        '</div>' +
        '</a>' +
        '</div>';
      $('.tbt_all_authors-list').append(html);
    });
  });
</script>


我想合并这两个代码来找到作者的评论号。如果作者有任何评论,它应该自动显示编号。我不知道如何合并这两个代码,以显示作者的评论数,如果作者有任何评论。请有人帮我找出来。我尝试了很多方法,但都不起作用。

de90aj5v

de90aj5v1#

你在html元素中使用了sfwCommentCount变量,但它超出了这个范围,这就是为什么它不起作用

<link href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet" />
<script crossorigin="anonymous" src="https://kit.fontawesome.com/7dfc182d96.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<style>
    .author-image {
        cursor: pointer;
    }
</style>

<div class="mb-0 mt-12">
    <h4 class="mb-0 text-black dark:text-gray-300"><i class="fa-solid fa-user-vneck-hair"></i> Authors and Writers</h4>
</div>
<div
    class="tbt_all_authors-list mt-0 mb-16 grid grid-cols-1 sm:grid-cols-1 md:grid-cols-3 lg:grid-cols-3 xl:grid-cols-3 gap-5"></div>

<script>
    let sfwCommentCountAll = 0;
    function displayCommentCount(data) {
        var sfwCommentCount = 0;
        if ("entry" in data.feed) {
            totalCommentCount = data.feed.entry.length;
            for (var i = 0; i < data.feed.entry.length; i++) {
                var authorName = data.feed.entry[i].author[0].name.$t;
                if (authorName === author.name) {
                    sfwCommentCount++;
                }
            }
        }
        sfwCommentCountAll = sfwCommentCount;
    }

    function handleJsonpData(data) {
        displayCommentCount(data);
    }
    var script = document.createElement("script");
    script.src =
        "https://tailwindbt.blogspot.com/feeds/comments/default?alt=json-in-script&max-results=10&callback=handleJsonpData";
    document.body.appendChild(script);

    let feedURL = "https://tailwindbt.blogspot.com/feeds/posts/default?alt=json-in-script&callback=?&max-results=10";
    $.getJSON(feedURL, function (data) {
        let authors = [];
        $.each(data.feed.entry, function (index, entry) {
            if (entry.author) {
                let authorName = entry.author[0].name.$t;
                let authorImage = entry.author[0].gd$image.src;
                let authorAbout = "";
                if (entry.author[0].gd$about) {
                    authorAbout = entry.author[0].gd$about.$t;
                }
                let authorProfileUrl = entry.author[0].uri.$t; // Extract the author profile URL
                let authorExists = false;
                let authorIndex;
                $.each(authors, function (index, author) {
                    if (author.name === authorName) {
                        authorExists = true;
                        authorIndex = index;
                    }
                });
                if (authorExists) {
                    authors[authorIndex].count++;
                } else {
                    authors.push({
                        name: authorName,
                        image: authorImage,
                        count: 1,
                        about: authorAbout,
                        profileUrl: authorProfileUrl,
                    });
                }
            }
        });

        authors.sort(function (a, b) {
            return b.count - a.count;
        });

        $.each(authors, function (index, author) {
            let html =
                '<div class="flex bg-white dark:bg-gray-700 dark:text-gray-300 shadow rounded">' +
                '<a href="' +
                author.profileUrl +
                '" target="_blank" class="author-image flex items-start px-3 py-3">' +
                '<div class="w-20 h-20 rounded-full object-cover mr-4 shadow" style="background-image: url(' +
                author.image +
                '); background-repeat: no-repeat; background-position: center; background-size: cover;"></div>' +
                '<div class="">' +
                '<div class="flex items-center justify-between">' +
                '<div class="text-md font-semibold text-gray-900 dark:text-gray-300 mt-3">' +
                author.name +
                "</div>" +
                "</div>" +
                '<div class="text-gray-700 dark:text-gray-400">Posts: ' +
                author.count +
                " </div>" +
                '<div class="text-gray-700 dark:text-gray-400">Comments: ' +
                sfwCommentCountAll +
                " </div>" +
                "</div>" +
                "</a>" +
                "</div>";

            $(".tbt_all_authors-list").append(html);
        });
    });
</script>

字符串

相关问题