4.样式处理-class.html 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. <script src="../../vue.js"></script>
  9. <style>
  10. /* 激活样式 */
  11. .active {
  12. color: #556b2f;
  13. font-size: 18px;
  14. }
  15. /* 异常样式 */
  16. .error {
  17. color: #b22222;
  18. }
  19. .common {
  20. font-weight: 700;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="app">
  26. <!-- v-bind指令 -可以动态绑定html元素属性- v-bind:attrName="js表达式" -->
  27. <!-- 样式处理:class & style -->
  28. <p class="common" v-bind:class="{active: isActive, error: true}">
  29. 对象语法绑定class: 动态绑定的样式类名 会和 静态绑定的类名 合并。而不是
  30. 覆盖
  31. </p>
  32. <p v-bind:class="['common', 'active']">数组语法绑定class:</p>
  33. <p v-bind:class="['common', {active: isActive}]">
  34. 数组语法中 混合使用 对象语法
  35. </p>
  36. </div>
  37. <script>
  38. var vm = new Vue({
  39. el: '#app',
  40. data: {
  41. isActive: false,
  42. },
  43. });
  44. </script>
  45. </body>
  46. </html>