07.JSON表示方式-对象表示.html 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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>Document</title>
  8. </head>
  9. <body>
  10. <h2>员工信息</h2>
  11. <table border="1" width="400" class="employee">
  12. <tr>
  13. <th>员工编号</th>
  14. <th>姓名</th>
  15. <th>性别</th>
  16. <th>年龄</th>
  17. <th>家庭住址</th>
  18. </tr>
  19. </table>
  20. <ul class="score">
  21. </ul>
  22. <table border="1" width="800" class="stu">
  23. <caption>学生成绩表</caption>
  24. <thead>
  25. <tr>
  26. <th>学号</th>
  27. <th>姓名</th>
  28. <th>性别</th>
  29. <th>年龄</th>
  30. <th>成绩</th>
  31. <th>住址</th>
  32. </tr>
  33. </thead>
  34. </table>
  35. </body>
  36. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  37. <script>
  38. // 单值
  39. var productName = '牙膏';
  40. var price = 2.5;
  41. // 1、对象表示
  42. var obj = {
  43. id: '123456',
  44. name: '孙悟空',
  45. sex: '男',
  46. age: 18,
  47. addr: '中国花果山'
  48. };
  49. // console.log(obj);
  50. var str = '<tr><td>' + obj.id + '</td><td>' + obj.name +
  51. '</td><td>' + obj.sex + '</td><td>' + obj.age +
  52. '</td><td>' + obj.addr + '</td></tr>';
  53. $('.employee').append(str);
  54. // 2、数组
  55. var score = [98, 90, 100, 99, 96, 97]
  56. score.forEach(function (value, index) {
  57. console.log(value)
  58. $('.score').append('<li>' + value + '</li>')
  59. })
  60. // 3、复杂表示法(对象+数组)
  61. var students = [
  62. {
  63. id: '123456',
  64. name: '孙悟空',
  65. sex: '男',
  66. age: 18,
  67. score: [98, 90, 100, 99, 96, 97],
  68. addr: '中国花果山'
  69. },
  70. {
  71. id: '234567',
  72. name: '白骨精',
  73. sex: '女',
  74. age: 19,
  75. score: [88, 90, 80, 99, 92, 98],
  76. addr: '荒山野岭'
  77. },
  78. {
  79. id: '345678',
  80. name: '猪八戒',
  81. sex: '男',
  82. age: 28,
  83. score: [98, 90, 100, 99, 96, 97],
  84. addr: '福临山云栈洞'
  85. }
  86. ];
  87. for(var i in students){
  88. console.log(students[i])
  89. var data = '<tr><td>'+students[i].id+'</td><td>'+students[i].name+
  90. '</td><td>'+students[i].sex+'</td><td>'+students[i].age+
  91. '</td><td>'+students[i].score.join(' ')+'</td><td>'+students[i].addr+'</td></tr>'
  92. $('.stu').prepend(data)
  93. }
  94. </script>
  95. </html>