练习题4_商品列表.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .product-card {
  9. width: 234px;
  10. /* margin: 100px auto; */
  11. /* 如果数值为0 那么单位可以省略 如果值是0.X 0也可以省略 0.5=.5 */
  12. box-shadow: 0 0 10px rgba(0, 0, 0, .5);
  13. padding: 10px;
  14. text-align: center;
  15. float: left;
  16. margin-right: 20px;
  17. }
  18. .product-img img {
  19. width: 100%;
  20. }
  21. .product-title {
  22. /* 文字大小尽量不要小于12px */
  23. font-size: 14px;
  24. font-weight: 400;
  25. color: #333;
  26. }
  27. .product-info {
  28. font-size: 12px;
  29. color: #b0b0b0;
  30. text-wrap: nowrap;
  31. overflow: hidden;
  32. text-overflow: ellipsis;
  33. margin-top: 10px;
  34. margin-bottom: 20px;
  35. }
  36. .product-price span {
  37. color: #ff6700;
  38. }
  39. .product-price del {
  40. color: #b0b0b0;
  41. }
  42. </style>
  43. </head>
  44. <body>
  45. <div class="product-list"></div>
  46. <!-- <div class="product-card">
  47. <div class="product-img">
  48. <img src="./img/phone2.png" alt="img">
  49. </div>
  50. <div class="product-title">REDMI Turbo 5 MAX</div>
  51. <div class="product-info">天玑 9500s | 9000mAh 最大小米电池 | 1.5K 超级阳光屏</div>
  52. <div class="product-price">
  53. <span>2299元起</span>
  54. <del>2499元</del>
  55. </div>
  56. </div> -->
  57. <script>
  58. // 获取数据
  59. let xhr = new XMLHttpRequest();
  60. xhr.open("GET", "http://shop-api.edu.koobietech.com/prod/tagProdList");
  61. xhr.send();
  62. xhr.onreadystatechange = function () {
  63. if (xhr.readyState == 4 && xhr.status == 200) {
  64. // 解析响应体
  65. let data = JSON.parse(xhr.responseText);
  66. data = data.data[0].productDtoList;
  67. console.log(data);
  68. // 渲染商品列表
  69. let productHTML = ``;
  70. // 渲染商品列表
  71. for(let i=0;i<data.length;i++){
  72. productHTML += `
  73. <div class="product-card">
  74. <div class="product-img">
  75. <img src="${data[i].pic}" alt="img">
  76. </div>
  77. <div class="product-title">${data[i].prodName}</div>
  78. <div class="product-info">${data[i].brief}</div>
  79. <div class="product-price">
  80. <span>${data[i].price}元起</span>
  81. </div>
  82. </div>`
  83. }
  84. // 渲染商品列表到页面中
  85. let productList = document.querySelector(".product-list");
  86. productList.innerHTML = productHTML;
  87. }
  88. }
  89. </script>
  90. </body>
  91. </html>