【问题标题】:Get all data from api and insert into HTML从 api 获取所有数据并插入 HTML
【发布时间】:2021-05-10 13:59:35
【问题描述】:

我的 html 中有 7 张卡片,包括图片、标题、描述和来源列。我正在尝试从 gnews api 获取数据,然后用从 api 获得的数据填充我的卡片。我想要来自 api 的数据来交换我的占位符文本和图像。但我只能在我的 HTML 中交换第一张卡片中的详细信息。

API 中的 JSON 如下所示:

"articles": [
        {
            "title": "xxx xxx",
            "description": "xxx xxx xxx",
            "content": "xxx xxx xxx xxx... [1514 chars]",
            "url": "xxx xxx",
            "image": "xxx.jpg",
            "publishedAt": "xxx",
            "source": {
                "name": "xxx",
                "url": "xxx.com/"
            }
        }
    ]

我尝试了一个 forEach,但它只得到了第一张卡片

   const title = document.querySelectorAll(".title"),
   image = document.querySelector(".image"),
   source = document.querySelector(".source"),
   url = document.querySelector(".url"); 
   
   .........
   .then(function(data){
   data.articles.forEach(article => {
            title.innerHTML = article.title;
            image.style.backgroundImage = article.image;
           source.innerHTML = article.source.name;
           url.setAttribute('href', article.url);
            url.setAttribute('target', '_blank');
          });
}


// And also i tried to target every card individually which worked but that means i would have
// to target all 7 html cards that i have which would be quite cumbersome and repetitive to write.
// Below is the code for the individual targetting..

           news.title = data.articles[0].title;
           news.image = data.articles[0].image;
           news.source = data.articles[0].source.name;
           news.url = data.articles[0].url;
           news.description = data.articles[0].description;

我如何编写代码以便能够从所有文章 JSON 中获取数据并同时用不同的新闻填充所有其他卡片??

【问题讨论】:

    标签: javascript html arrays api dom


    【解决方案1】:

    您好,欢迎来到社区。​​p>

    看来你还有很多工作要做。 好吧,您必须首先考虑这一点:您必须遍历所有卡片和所有组件,这是您没有做的事情。

    意思是这样的:

       .then(function(data){
       data.articles.forEach(article => {
                title.innerHTML = article.title;
                image.style.backgroundImage = article.image;
               source.innerHTML = article.source.name;
               url.setAttribute('href', article.url);
                url.setAttribute('target', '_blank');
              //some code to skip to another card
              });
    

    请记住

       const title = document.querySelectorAll(".title"),
       image = document.querySelector(".image"),
       source = document.querySelector(".source"),
       url = document.querySelector(".url"); 
    

    所有这些都是检索元素数组,因为你有七个,对吧? 在您的代码中,您继续指向图像、标题来源和 url,没有任何索引指示。如果是这种情况,您将不断更新第一个。

    尝试这样做:

       .then(function(data){
       let x=0;
       data.articles.forEach(article => {
                title[x].innerHTML = article.title;
                image[x].style.backgroundImage = article.image;
               source[x].innerHTML = article.source.name;
               url[x].setAttribute('href', article.url);
                url[x].setAttribute('target', '_blank');
              x++
              });
    

    不过,这并不是一个完美的解决方案。

    看看this page并仔细阅读。花一些时间去做——我做到了!如果还是不明白,请告诉我!

    【讨论】:

    • 感谢您的回复。谢天谢地,卡片现已更新,但控制台出现错误。 Uncaught (in promise) TypeError: Cannot set property 'innerHTML' of undefined at news.js:26 at Array.forEach (<anonymous>) at news.js:20 我在控制台记录了描述,我在 const 中声明了它,它显示它正在从 7 张卡片中的 6 张获取详细信息,但最后一张卡片显示为未定义... 请注意所有 7 张卡片都有带有描述类别的 ap 标签。您认为这里可能存在什么问题?
    • 抱歉耽搁了……您检查过 stuff[x] 不为空吗?当我说 stuff[x] 时,它可能是 title[x]、image[x] 或您尝试更改的任何其他元素。
    猜你喜欢
    • 2018-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    • 2017-05-16
    • 1970-01-01
    • 2014-09-17
    • 2023-03-10
    相关资源
    最近更新 更多