8_v-bind.html 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. .active{
  16. font-size: 40px;
  17. font-weight: bolder;
  18. color: green;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div id="app">
  24. <!-- 默认情况下在vue中标签上属性的值不能使用变量 -->
  25. <!-- 如果需要使用变量那么需要在属性前方加上v-bind 来声明属性使用的是变量 -->
  26. <div v-bind:class="styleName">hello</div>
  27. <button @click="changeClass('box1')">box1</button>
  28. <button @click="changeClass('box2')">box2</button>
  29. <!-- <img v-bind:src="imgSrc" alt=""> -->
  30. <!-- <img v-bind:src="imgSrc + '.jpg'" alt=""> -->
  31. <div v-bind:class="{ active : isActive }">你好</div>
  32. <div :style="{ fontSize:fontS+'px' }"> 世界 </div>
  33. </div>
  34. <script>
  35. var app = new Vue({
  36. el:"#app",
  37. data:{
  38. styleName:"",
  39. imgSrc:"./img/1",
  40. isActive:false,
  41. fontS:60
  42. },
  43. methods: {
  44. changeClass: function (cName) {
  45. this.styleName = cName;
  46. }
  47. },
  48. })
  49. </script>
  50. </body>
  51. </html>