简体   繁体   English

如何使用 HTML 表在 HTML 表上显示所有 JSON 获取 API 内容

[英]How to display all JSON Fetch API content on a HTML table using JavaScript

I'm starting to learn JavaScript, and i am having some difficulties for displaying the whole data i got from Json PlaceHolder fake API on my html table. I'm starting to learn JavaScript, and i am having some difficulties for displaying the whole data i got from Json PlaceHolder fake API on my html table. Using the code below i just manage to display the last element from the API itself, and not all elements as i wanted.使用下面的代码,我只是设法显示 API 本身的最后一个元素,而不是我想要的所有元素。

Can someone help me figure it out how should i actually do?有人可以帮我弄清楚我应该怎么做吗?

There's the code:有代码:

fetch('https://jsonplaceholder.typicode.com/posts')
  .then((response) => response.json())
  .then((json) => json.forEach(tableElements)
  )

  function tableElements (element, index, arr){
    arr[index] = document.querySelector('#posts-table tbody').innerHTML = 
    `<td>${element.userId}</td>
    <td>${element.id}</td>
    <td>${element.title}</td>
    <td>${element.body}</td>`
  }

Your code is showing one data because when the loop goes it are overwrites the previous one when it loop through the json, change the opperand to += and wrap the table columns with rows <tr>... </tr>您的代码显示一个数据,因为当循环进行时,它会在循环通过 json 时覆盖前一个数据,将操作数更改为+=并用行<tr>... </tr>包装表列
Try this code here在此处尝试此代码

fetch('https://jsonplaceholder.typicode.com/posts')
  .then((response) => response.json())
  .then((json) => json.forEach(tableElements)
  )

  function tableElements (element, index, arr){
    arr[index] = document.querySelector('#posts-table tbody').innerHTML +=
    `<tr>
        <td>${element.userId}</td>
        <td>${element.id}</td>
        <td>${element.title}</td>
        <td>${element.body}</td>
    </tr>`
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 JavaScript 将 JSON 显示到 html 表中? - How to display a JSON into an html table using JavaScript? 获取 JSON 并显示 HTML 表 - Fetch JSON and display HTML table 从 JSON 获取数据并使用 JavaScript 显示在 HTML 表上以传递数据 - Fetch data from JSON and display on HTML table using JavaScript to pass the data 使用 Fetch API javascript 将 Content-Type 设置为 application/Json - set Content-Type to application/Json using Fetch API javascript 如何在 Javascript 中仅获取(使用 Fetch API)标题,而不是整个内容? - How to fetch (using Fetch API) only headers in Javascript, not the whole content? 使用纯 javascript 和引导程序:如何显示模式对话框确认来自 JSON API 的提取操作的响应 - Using pure javascript and Bootstrap : How to display a modal dialog confirm response from fetch operation from JSON API 如何使用Javascript或jQuery显示html内容? - How to display html content using Javascript or jQuery? 如何使用Javascript查看所有html内容? - How to view all html content using Javascript? 如何使用javascript将json数据显示为html - how to display json data into html using javascript 如何使用 Javascript 循环在 JSON 文件中获取和显示特定索引 - How to fetch and display a specific index in a JSON file using a Javascript loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM