5.练习.html 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. </head>
  8. <body>
  9. <style>
  10. button {
  11. margin-top: 10px;
  12. }
  13. </style>
  14. <div id="root">
  15. <h1>学生信息</h1>
  16. <button @click="addAge">年龄+1岁</button> <br />
  17. <button @click="addSex">添加性别属性,默认值:男</button> <br />
  18. <button @click="updateSex">修改性别</button> <br />
  19. <button @click="addFriend">在列表首位添加一个朋友</button> <br />
  20. <button @click="updateFirstFriendName">
  21. 修改第一个朋友的名字为:张三
  22. </button>
  23. <br />
  24. <button @click="addHobby">添加一个爱好</button> <br />
  25. <button @click="updateHobby">修改第一个爱好为:开车</button> <br />
  26. <button @click="removeSmoke">过滤掉爱好中的抽烟</button> <br />
  27. <h3>姓名:{{ student.name }}</h3>
  28. <h3>年龄:{{ student.age }}</h3>
  29. <h3 v-if="student.sex">性别:{{student.sex}}</h3>
  30. <h3>爱好:</h3>
  31. <ul>
  32. <li v-for="(h,index) in student.hobby" :key="index">{{ h }}</li>
  33. </ul>
  34. <h3>朋友们:</h3>
  35. <ul>
  36. <li v-for="(f,index) in student.friends" :key="index">
  37. {{ f.name }}--{{ f.age }}
  38. </li>
  39. </ul>
  40. </div>
  41. <script src="./vue.js"></script>
  42. <script type="text/javascript">
  43. const vm = new Vue({
  44. el: "#root",
  45. data: {
  46. student: {
  47. name: "tom",
  48. age: 18,
  49. hobby: ["抽烟", "喝酒", "烫头"],
  50. friends: [
  51. { name: "jerry", age: 35 },
  52. { name: "tony", age: 36 },
  53. ],
  54. },
  55. },
  56. methods: {
  57. addAge() {
  58. this.student.age++;
  59. },
  60. addSex() {
  61. this.$set(this.student,'sex','男');
  62. },
  63. updateSex() {
  64. this.$set(this.student,'sex','未知');
  65. },
  66. addFriend() {
  67. this.student.friends.unshift({name:"Jack",age:21});
  68. },
  69. updateFirstFriendName() {
  70. this.student.friends[0].name = '张三';
  71. },
  72. addHobby() {
  73. this.student.hobby.push("于谦");
  74. },
  75. updateHobby() {
  76. this.$set(this.student.hobby,0,'开车');
  77. },
  78. removeSmoke() {
  79. // this.student.hobby = this.student.hobby.filter((item) => {
  80. // return item != '抽烟';
  81. // })
  82. this.student.hobby.splice(0,1)
  83. }
  84. },
  85. });
  86. </script>
  87. </body>
  88. </html>