8_v-bind.html 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. <script src="./js/vue.js"></script>
  8. <style>
  9. .box1{
  10. color: red;
  11. }
  12. .box2{
  13. color: blue;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="app">
  19. <!-- 默认情况下在vue中标签上属性的值不能使用变量 -->
  20. <!-- 如果需要使用变量那么需要在属性前方加上v-bind 来声明属性使用的是变量 -->
  21. <div v-bind:class="styleName">hello</div>
  22. <button @click="changeClass('box1')">box1</button>
  23. <button @click="changeClass('box2')">box2</button>
  24. <!-- <img v-bind:src="imgSrc" alt=""> -->
  25. <img v-bind:src="imgSrc + '.jpg'" alt="">
  26. </div>
  27. <script>
  28. var app = new Vue({
  29. el:"#app",
  30. data:{
  31. styleName:"",
  32. imgSrc:"./img/1"
  33. },
  34. methods: {
  35. changeClass: function (cName) {
  36. this.styleName = cName;
  37. }
  38. },
  39. })
  40. </script>
  41. </body>
  42. </html>