Blogger Information
Blog 24
fans 0
comment 0
visits 10949
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
fetch的使用
皮皮志
Original
494 people have browsed it
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. table {
  10. border-collapse: collapse;
  11. text-align: center;
  12. }
  13. table caption {
  14. font-size: 20px;
  15. margin-bottom: 10px;
  16. }
  17. table td {
  18. border-bottom: thin solid #888;
  19. padding: 5px;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <button onclick="getTable()">fetch</button>
  25. <table>
  26. <caption>fetch请求数据</caption>
  27. <thead>
  28. <tr>
  29. <td>userIdd</td>
  30. <td>id</td>
  31. <td>title</td>
  32. <td>completed</td>
  33. </tr>
  34. </thead>
  35. </table>
  36. <script>
  37. const table = document.querySelector('table')
  38. const tbody =table.createTBody()
  39. async function getTable() {
  40. let url = "https://jsonplaceholder.typicode.com/todos"
  41. const response = await fetch(url);
  42. const result = await response.json();
  43. result.forEach(json => {
  44. const tr = `
  45. <tr>
  46. <td>${json.userId}</td>
  47. <td>${json.id}</td>
  48. <td>${json.title}</td>
  49. <td>${json.completed}</td>
  50. </tr>
  51. `
  52. tbody.insertAdjacentHTML("beforeend",tr)
  53. });
  54. }
  55. </script>
  56. </body>
  57. </html>

代码展示

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post