example_of_jquery_5.html 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Ajax请求</title>
  6. <style>
  7. #emp {
  8. border-collapse: collapse;
  9. }
  10. #emp td, #emp th {
  11. border-bottom: 1px solid black;
  12. width: 120px;
  13. text-align: center;
  14. }
  15. #page {
  16. width: 720px;
  17. text-align: center;
  18. }
  19. #page a {
  20. text-decoration: none;
  21. color: darkcyan;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <table id="emp">
  27. <thead>
  28. <tr>
  29. <th>编号</th>
  30. <th>姓名</th>
  31. <th>职位</th>
  32. <th>工资</th>
  33. <th>补贴</th>
  34. <th>所在部门</th>
  35. </tr>
  36. </thead>
  37. <tbody>
  38. </tbody>
  39. </table>
  40. <div id="page">
  41. <a id="prev" href="">上一页</a>&nbsp;&nbsp;
  42. <a id="next" href="">下一页</a>
  43. </div>
  44. <script src="js/jquery.min.js"></script>
  45. <script>
  46. $(() => {
  47. function getEmpData(url) {
  48. $.ajax({
  49. url: url,
  50. type: 'get',
  51. data: {},
  52. dataType: 'json',
  53. headers: {
  54. "token": "35ad60445cea11e99e1000163e02b646"
  55. },
  56. success: (json) => {
  57. $('#emp>tbody').empty()
  58. json.results.forEach((emp) => {
  59. let tr = $('<tr>')
  60. .append($('<td>').text(emp.no))
  61. .append($('<td>').text(emp.name))
  62. .append($('<td>').text(emp.job))
  63. .append($('<td>').text(emp.sal))
  64. .append($('<td>').text(emp.comm))
  65. .append($('<td>').text(emp.dept.name))
  66. $('#emp>tbody').append(tr)
  67. })
  68. $('#prev').attr('href', json.previous? json.previous : '')
  69. $('#next').attr('href', json.next? json.next : '')
  70. }
  71. })
  72. }
  73. function changePage(evt) {
  74. evt.preventDefault()
  75. let href = $(evt.target).attr('href')
  76. if (href) {
  77. getEmpData(href)
  78. }
  79. }
  80. getEmpData('http://120.77.222.217/api/emps/')
  81. $('#prev').on('click', changePage)
  82. $('#next').on('click', changePage)
  83. })
  84. </script>
  85. </body>
  86. </html>