练习题4_商品卡.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. .box{
  9. display: flex;
  10. flex-wrap: wrap;
  11. }
  12. .goods-card {
  13. width: 234px;
  14. box-shadow: 0 0 5px gray;
  15. padding-bottom: 30px;
  16. margin-left: 20px;
  17. margin-bottom: 20px;
  18. }
  19. .goods-pic img {
  20. width: 234px;
  21. }
  22. .goods-title,
  23. .goods-info,
  24. .goods-price {
  25. padding: 0 10px;
  26. }
  27. .goods-title {
  28. color: #333;
  29. font-size: 14px;
  30. font-weight: 400;
  31. text-align: center;
  32. margin-bottom: 2px;
  33. }
  34. .goods-info {
  35. font-size: 12px;
  36. color: #b0b0b0;
  37. overflow: hidden;
  38. text-overflow: ellipsis;
  39. text-wrap: nowrap;
  40. margin-bottom: 10px;
  41. }
  42. .goods-price {
  43. text-align: center;
  44. }
  45. .goods-price span {
  46. color: #ff6700;
  47. font-size: 14px;
  48. font-weight: 400;
  49. }
  50. .goods-price del {
  51. color: #b0b0b0;
  52. font-size: 14px;
  53. font-weight: 400;
  54. }
  55. </style>
  56. </head>
  57. <body>
  58. <div class="box">
  59. </div>
  60. <script>
  61. // 第一个步创建XMLHttpRequest对象
  62. let xhr = new XMLHttpRequest();
  63. // 第二个步调用open方法配置请求信息
  64. xhr.open("GET", "http://shop-api.edu.koobietech.com/prod/tagProdList");
  65. // 第三步发送请求
  66. xhr.send();
  67. // 第四步监听响应
  68. xhr.onreadystatechange = function () {
  69. if (xhr.readyState == 4 && xhr.status == 200) {
  70. let jsonStr = xhr.responseText;
  71. // 服务端返回的数据为JSON字符串
  72. // 把JSON字符串转换为对象
  73. let obj = JSON.parse(jsonStr);
  74. console.log(obj.data[0].productDtoList);
  75. let productDtoList = obj.data[0].productDtoList;
  76. let htmlStr = ""
  77. let oBox = document.querySelector(".box");
  78. productDtoList.forEach(function (val) {
  79. htmlStr += `
  80. <div class="goods-card">
  81. <div class="goods-pic">
  82. <img src="${val.pic}" alt="phone">
  83. </div>
  84. <div class="goods-title">
  85. <span>${val.prodName}</span>
  86. </div>
  87. <div class="goods-info">
  88. ${val.brief}
  89. </div>
  90. <div class="goods-price">
  91. <span>${val.price}元起</span>
  92. </div>
  93. </div>
  94. `;
  95. });
  96. oBox.innerHTML = htmlStr;
  97. }
  98. }
  99. </script>
  100. </body>
  101. </html>