• AJAX——案例


    1.商品分类

    需求:尽可能同时展示所有商品分类到页面上

    步骤:

    1. 获取所有的一级分类数据
    2. 遍历id,创建获取二级分类请求
    3. 合并所有二级分类Promise对象
    4. 等待同时成功后,渲染页面

    index.html代码

    1. <!DOCTYPE html>
    2. <html lang="en">
    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>案例_分类导航</title>
    8. <link rel="stylesheet" href="./css/index.css">
    9. </head>
    10. <body>
    11. <!-- 大容器 -->
    12. <div class="container">
    13. <div class="sub-list">
    14. <div class="item">
    15. <h3>分类名字</h3>
    16. <ul>
    17. <li>
    18. <a href="javascript:;">
    19. <img src="http://zhoushugang.gitee.io/erabbit-client-pc-static/uploads/img/category%20(9).png" />
    20. <p>巧克力</p>
    21. </a>
    22. </li>
    23. <li>
    24. <a href="javascript:;">
    25. <img src="http://zhoushugang.gitee.io/erabbit-client-pc-static/uploads/img/category%20(9).png" />
    26. <p>巧克力</p>
    27. </a>
    28. </li>
    29. <li>
    30. <a href="javascript:;">
    31. <img src="http://zhoushugang.gitee.io/erabbit-client-pc-static/uploads/img/category%20(9).png" />
    32. <p>巧克力</p>
    33. </a>
    34. </li>
    35. </ul>
    36. </div>
    37. </div>
    38. </div>
    39. <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    40. <script>
    41. /**
    42. * 目标:把所有商品分类“同时”渲染到页面上
    43. * 1. 获取所有一级分类数据
    44. * 2. 遍历id,创建获取二级分类请求
    45. * 3. 合并所有二级分类Promise对象
    46. * 4. 等待同时成功后,渲染页面
    47. */
    48. // 1. 获取所有一级分类数据
    49. axios({
    50. url:'http://hmajax.itheima.net/api/category/top'
    51. }).then(result => {
    52. console.log(result)
    53. // 2. 遍历id,创建获取二级分类请求
    54. const secPromiseList = result.data.data.map(item => {
    55. return axios({
    56. url:'http://hmajax.itheima.net/api/category/sub',
    57. params: {
    58. id: item.id // 一级分类id
    59. }
    60. })
    61. })
    62. console.log(secPromiseList) // [二级分类请求Promise对象,二级分类请求Promise对象……]
    63. // 3. 合并所有二级分类Promise对象
    64. const p = Promise.all(secPromiseList)
    65. p.then(result => {
    66. console.log(result)
    67. // 4. 等待同时成功后,渲染页面
    68. const htmlStr = result.map(item => {
    69. const dataObj = item.data.data // 取出关键数据对象
    70. return `<div class="item">
    71. <h3>${dataObj.name}</h3>
    72. <ul>
    73. ${dataObj.children.map(item => {
    74. return `<li>
    75. <a href="javascript:;">
    76. <img src="${item.picture}">
    77. <p>${item.name}</p>
    78. </a>
    79. </li>`
    80. }).join('')}
    81. </ul>
    82. </div>`
    83. }).join('')
    84. console.log(htmlStr)
    85. document.querySelector('.sub-list').innerHTML = htmlStr
    86. })
    87. })
    88. </script>
    89. </body>
    90. </html>

    index.css代码

    1. * {
    2. margin: 0;
    3. padding: 0;
    4. box-sizing: border-box;
    5. }
    6. a {
    7. text-decoration: none;
    8. color: #333;
    9. }
    10. ul {
    11. list-style: none;
    12. }
    13. .container {
    14. width: 980px;
    15. margin: 0 auto;
    16. }
    17. .container h3 {
    18. font-size: 18px;
    19. color: #666;
    20. font-weight: normal;
    21. text-align: center;
    22. line-height: 100px;
    23. }
    24. .container .sub-list {
    25. background-color: #fff;
    26. }
    27. .container .sub-list ul {
    28. display: flex;
    29. padding: 0 32px;
    30. flex-wrap: wrap;
    31. }
    32. .container .sub-list ul li {
    33. width: 168px;
    34. height: 160px;
    35. }
    36. .container .sub-list ul li a {
    37. text-align: center;
    38. display: block;
    39. font-size: 14px;
    40. }
    41. .container .sub-list ul li a img {
    42. width: 100px;
    43. height: 100px;
    44. }
    45. .container .sub-list ul li a p {
    46. line-height: 40px;
    47. }
    48. .container .sub-list ul li a:hover {
    49. color: var(--xtx-color);
    50. }
    51. .ref-goods {
    52. background-color: #fff;
    53. margin-top: 20px;
    54. position: relative;
    55. }
    56. .ref-goods .head .xtx-more {
    57. position: absolute;
    58. top: 20px;
    59. right: 20px;
    60. }
    61. .ref-goods .head .tag {
    62. text-align: center;
    63. color: #999;
    64. font-size: 20px;
    65. position: relative;
    66. top: -20px;
    67. }
    68. .ref-goods .body {
    69. display: flex;
    70. justify-content: flex-start;
    71. flex-wrap: wrap;
    72. padding: 0 65px 30px;
    73. }
    74. .ref-goods .body .none {
    75. height: 220px;
    76. text-align: center;
    77. width: 100%;
    78. line-height: 220px;
    79. color: #999;
    80. }

    2.学习反馈-省市区切换

    需求:完成省市区切换效果

    步骤:

    1. 设置省份数据到下拉菜单
    2. 切换省份,设置城市数据到下拉菜单,并清空地区下拉菜单
    3. 切换城市,设置地区数据到下拉菜单

    index.js

    1. /**
    2. * 目标1:完成省市区下拉列表切换
    3. * 1.1 设置省份下拉菜单数据
    4. * 1.2 切换省份,设置城市下拉菜单数据,清空地区下拉菜单
    5. * 1.3 切换城市,设置地区下拉菜单数据
    6. */
    7. // 1.1 设置省份下拉菜单数据
    8. axios({
    9. url: 'http://hmajax.itheima.net/api/province'
    10. }).then(result => {
    11. const optionStr = result.data.list.map(pname => `<option value="${pname}">${pname}</option>`).join('')
    12. document.querySelector('.province').innerHTML = `<option value="">省份</option>` + optionStr
    13. })
    14. // 1.2 切换省份,设置城市下拉菜单数据,清空地区下拉菜单
    15. document.querySelector('.province').addEventListener('change', async e => {
    16. // 获取用户选择省份名字
    17. // console.log(e.target.value)
    18. const result = await axios({ url: 'http://hmajax.itheima.net/api/city', params: { pname: e.target.value } })
    19. const optionStr = result.data.list.map(cname => `<option value="${cname}">${cname}</option>`).join('')
    20. // 把默认城市选项+下属城市数据插入select
    21. document.querySelector('.city').innerHTML = `<option value="">城市</option>` + optionStr
    22. // 清空地区数据
    23. document.querySelector('.area').innerHTML = `<option value="">地区</option>`
    24. })
    25. // 1.3 切换城市,设置地区下拉菜单数据
    26. document.querySelector('.city').addEventListener('change', async e => {
    27. console.log(e.target.value)
    28. const result = await axios({url: 'http://hmajax.itheima.net/api/area', params: {
    29. pname: document.querySelector('.province').value,
    30. cname: e.target.value
    31. }})
    32. console.log(result)
    33. const optionStr = result.data.list.map(aname => `<option value="${aname}">${aname}</option>`).join('')
    34. console.log(optionStr)
    35. document.querySelector('.area').innerHTML = `<option value="">地区</option>` + optionStr
    36. })
    37. /**
    38. * 目标2:收集数据提交保存
    39. * 2.1 监听提交的点击事件
    40. * 2.2 依靠插件收集表单数据
    41. * 2.3 基于axios提交保存,显示结果
    42. */
    43. // 2.1 监听提交的点击事件
    44. document.querySelector('.submit').addEventListener('click', async () => {
    45. // 2.2 依靠插件收集表单数据
    46. const form = document.querySelector('.info-form')
    47. const data = serialize(form, { hash: true, empty: true })
    48. console.log(data)
    49. // 2.3 基于axios提交保存,显示结果
    50. try {
    51. const result = await axios({
    52. url: 'http://hmajax.itheima.net/api/feedback',
    53. method: 'POST',
    54. data
    55. })
    56. console.log(result)
    57. alert(result.data.message)
    58. } catch (error) {
    59. console.dir(error)
    60. alert(error.response.data.message)
    61. }
    62. })

    index.html

    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. <!-- 初始化样式 -->
    8. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/reset.css@2.0.2/reset.min.css">
    9. <!-- 引入bootstrap.css -->
    10. <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.2.3/css/bootstrap.min.css" rel="stylesheet">
    11. <!-- 核心样式 -->
    12. <link rel="stylesheet" href="./css/index.css">
    13. <title>学习反馈</title>
    14. </head>
    15. <body>
    16. <div class="container">
    17. <h4 class="stu-title">学习反馈</h4>
    18. <img class="bg" src="./img/head.png" alt="">
    19. <div class="item-wrap">
    20. <div class="hot-area">
    21. <span class="hot">热门校区</span>
    22. <ul class="nav">
    23. <li><a target="_blank" href="http://bjcp.itheima.com/">北京</a> </li>
    24. <li><a target="_blank" href="http://sh.itheima.com/">上海</a> </li>
    25. <li><a target="_blank" href="http://gz.itheima.com/">广州</a> </li>
    26. <li><a target="_blank" href="http://sz.itheima.com/">深圳</a> </li>
    27. </ul>
    28. </div>
    29. <form class="info-form">
    30. <div class="area-box">
    31. <span class="title">地区选择</span>
    32. <select name="province" class="province">
    33. <option value="">省份</option>
    34. </select>
    35. <select name="city" class="city">
    36. <option value="">城市</option>
    37. </select>
    38. <select name="area" class="area">
    39. <option value="">地区</option>
    40. </select>
    41. </div>
    42. <div class="area-box">
    43. <span class="title">您的称呼</span>
    44. <input type="text" name="nickname" class="nickname" value="播仔">
    45. </div>
    46. <div class="area-box">
    47. <span class="title">宝贵建议</span>
    48. <textarea type="text" name="feedback" class="feedback" placeholder="您对AJAX阶段课程宝贵的建议"></textarea>
    49. </div>
    50. <div class="area-box">
    51. <button type="button" class="btn btn-secondary submit">
    52. 确定提交
    53. </button>
    54. </div>
    55. </form>
    56. </div>
    57. </div>
    58. <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.2.0/axios.min.js"></script>
    59. <script src="./js/form-serialize.js"></script>
    60. <!-- 核心代码 -->
    61. <script src="./js/index.js"></script>
    62. </body>
    63. </html>

    index.css

    1. .container {
    2. width: 1000px;
    3. padding-top: 20px;
    4. margin: 0 auto 0;
    5. position: relative;
    6. }
    7. .container .stu-title {
    8. font-weight: 900;
    9. font-size: 36px;
    10. }
    11. .container .bg {
    12. display: block;
    13. width: 100%;
    14. }
    15. .item-wrap .hot-area {
    16. display: flex;
    17. margin-bottom: 20px;
    18. }
    19. .item-wrap .hot-area .hot {
    20. color: #c32f32;
    21. font-weight: 600;
    22. margin-right: 20px;
    23. }
    24. .item-wrap .nav {
    25. display: flex;
    26. }
    27. .item-wrap .nav li {
    28. margin-right: 10px;
    29. }
    30. .item-wrap .nav li a {
    31. text-decoration: none;
    32. color: black;
    33. }
    34. .item-wrap .title {
    35. font-weight: 600;
    36. white-space: nowrap;
    37. margin-right: 20px;
    38. }
    39. .item-wrap select {
    40. width: 150px;
    41. height: 40px;
    42. font-size: 14px;
    43. color: black;
    44. letter-spacing: 0;
    45. font-weight: 400;
    46. background: #FFFFFF;
    47. border: 1px solid rgba(232, 232, 233, 1);
    48. border-radius: 4px;
    49. padding: 10px;
    50. outline: none;
    51. margin-right: 10px;
    52. }
    53. .item-wrap select option {
    54. font-weight: normal;
    55. display: block;
    56. white-space: nowrap;
    57. min-height: 1.2em;
    58. padding: 0px 2px 1px;
    59. font-size: 16px;
    60. }
    61. .item-wrap input {
    62. font-weight: normal;
    63. display: block;
    64. white-space: nowrap;
    65. min-height: 1.2em;
    66. padding: 0px 2px 1px;
    67. height: 40px;
    68. font-size: 16px;
    69. border: 1px solid rgba(232, 232, 233, 0.682);
    70. color: black;
    71. }
    72. .item-wrap .feedback {
    73. width: 400px;
    74. height: 150px;
    75. border: 1px solid rgba(232, 232, 233, 0.682);
    76. }
    77. .item-wrap .area-box {
    78. margin-bottom: 20px;
    79. display: flex;
    80. align-items: center;
    81. }
    82. .feedback::-webkit-input-placeholder {
    83. /* WebKit browsers */
    84. color: #BFBFBF;
    85. }
    86. .feedback:-moz-placeholder {
    87. /* Mozilla Firefox 4 to 18 */
    88. color: #BFBFBF;
    89. }
    90. .feedback::-moz-placeholder {
    91. /* Mozilla Firefox 19+ */
    92. color: #BFBFBF;
    93. }
    94. .feedback:-ms-input-placeholder {
    95. /* Internet Explorer 10+ */
    96. color: #BFBFBF;
    97. }

  • 相关阅读:
    41.说说Promise自身的静态方法
    Flutter实战-请求封装(一)
    给短视频添加上下图水印的话需要怎么批量操作
    【Java毕设】基于idea Java的在线考试系统(附源码+课件)
    eProsima Fast DDS(1)
    千亿IT运维市场,产品要凭实力说话
    加工制造业智慧采购系统解决方案:助力企业实现全流程采购一体化协同
    【数据结构】单链表的特点
    JVM-字节码
    JavaScript之BOM复习(54th)
  • 原文地址:https://blog.csdn.net/weixin_48719464/article/details/138098601