VUE学习日记(七) ---- class样式属性绑定 v-bind:class

v-bind:  简写可省略不写

如 v-bind:class="" 简写 :class=""

 

首先创建一个样式文件style.css

 

body{
    font-size:24px;
}
.active{
    color:red;
}
 
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>class绑定</title>
    <script src="https://unpkg.com/vue/dist/vue.js"></script>
    <link href="../vue_cli/css/style.css" rel="stylesheet">
  </head>
  <body>
    
    <div id="myPage">
      <div v-bind:class="{active:isActive}">测试文本内容</div>
      <div :class="{active:isActive}">测试文本内容</div>
      <button @click="btnclick">class绑定</button>
    </div>    

    <script>
      var myPage = new Vue({
        el:'#myPage',
        data:{
          isActive:false
        },
        methods:{
          btnclick:function()
          {
         //对当前值取反
              this.isActive=!this.isActive;
          }
        }
      })
    </script>

  </body>
</html>

 

效果图:

 

 点击按钮后,效果展示:

 

 

 

 

posted @ 2020-08-24 10:26  一介桃白白  阅读(162)  评论(0编辑  收藏  举报