05-v-bind动态绑定class(对象语法)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .active{
      color: red;
    }
  </style>
</head>
<body>
  <!-- 可能会有需求,在不同的情况对某个标签进行不同的样式处理。
        此时可以使用v-bind处理 -->
  <div id="app">
    <!-- <h2 class="active">{{message}}</h2> -->
    <!-- <h2 :class='active'>{{message}}</h2> -->

    <!-- <h2 v-bind:class="{样式类名1:boolean,样式类名2:boolean.....}"></h2> -->
    <!-- 这还支持静态class和动态class动态合并 -->
    <h2 class="title" :class="{active:isActive,line:isLine}">{{message}}</h2>
    <h2 class="title" :class="getClasss()">{{message}}</h2>
    <button @click="btnClick">切换</button>
  </div>
  <script src="../js/vue.js"></script>
  <script>
    const app=new Vue({
      el:'#app',
      data:{
        message:'你好啊',
        isActive:true,
        isLine:true
      },
      methods:{
        btnClick:function(){
          this.isActive=!this.isActive
        },
        getClasss(){
          return {active:this.isActive,line:this.isLine};
        }
      }
    })
  </script>
</body>
</html>

可结合检查(F12)的Elements和Console面板使用

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值