v-bind动态绑定class

v-bind的作用:动态地绑定一个或多个 attribute,或一个组件 prop 到表达式,v-bind可以简写为 :

v-bind可以动态绑定class,可以绑定对象或者为数组

绑定对象时:

  • v-bind可以绑定一组键-值,也可以绑定多组键-值;
  • v-bind动态绑定class时,不会影响标签原有的class;
  • v-bind也可以绑定方法,方法的内容为对象;
  • 绑定class的形式:v-bind="{键(string):值(boolean)}"

绑定数组时:

  • v-bind绑定方法的内容为数组;
  • 绑定class的形式:v-bind="[数组名称]"
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Vue计数器</title>
  <style>
    .active {
      color: blue;
    }
  </style>
</head>
<body>
<div id="app">
  <p v-bind:class="{active: isActive}">测试文本</p>
  <p :class="[test1, test2, 'active']">测试文本2</p>
  <button @click="btnChange">点击切换颜色</button>
</div>
<script src="../js/vue.js"></script>
<script>
  const app = new Vue({
    el: '#app',
    data: {
      message: '文本内容',
      isActive: true,
      test1: 'classP1',
      test2: 'classP2'
    },
    methods: {
      btnChange: function () {
        this.isActive = !this.isActive
      }
    }
  })
</script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值