class样式绑定.html 815 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <title></title>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <script src="https://unpkg.com/vue/dist/vue.js"></script>
  8. <link href="../css/style.css" rel="stylesheet"> </head>
  9. <body>
  10. <style>
  11. .active {
  12. width: 100px;
  13. height: 100px;
  14. background: green;
  15. }
  16. </style>
  17. <div id="myApp">
  18. <div v-bind:class="{ active: isActive }">红色文本1</div>
  19. <div :class="{ active: !isActive }">红色文本2</div>
  20. <button @click="btnClick">改变class吧</button>
  21. </div>
  22. <script>
  23. var myApp = new Vue({
  24. el: '#myApp',
  25. data: {
  26. isActive: true
  27. },
  28. methods: {
  29. btnClick: function() {
  30. this.isActive = !this.isActive;
  31. },
  32. },
  33. });
  34. </script>
  35. </body>
  36. </html>