VUE点击按钮切换字体颜色

前几天我朋友在学vue,在写一个关于点击按钮切换字体颜色的demo,我于是就看了一下,
按照我的想法我是这么写的
代码如下:

<template>
  <div>
    <p :class="{a: !isShow,b: isShow}">RUNNING MAN</p>
    <button @click="isShow=!isShow">点击</button>
  </div>
</template>

<script>
  export default {
    data(){
      return {
        isShow: true
      }
    }
  }
</script>

<style scoped>
  .a {
    color: #70DB55;
    font-size: 36px;
  }
  .b {
    color: #EA5656;
    font-size: 36px;
  }
</style>

本来想视频展示的,本人手残没传上来哈!只能截图看一下喽。
效果展示:
在这里插入图片描述
点击后切换效果为
在这里插入图片描述
其实我朋友也给我发了他的代码
他的代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>

  <style>
    .active{
      color: red;
    }
  </style>
</head>
<body>
<div id="app">
  <!--<h2 class="active">{{message}}</h2>
  <h2 :class="active">{{message}}</h2>-->
  <!--<h2 v-bind:class="{key1:value1,key2:value2}">{{message}}</h2>-->
  <!--<h2 v-bind:class="{类名1:true,类名2:boolean}">{{message}}</h2>-->
  <h2 class="title" v-bind:class="{active:isactive,line:isLine}">{{message}}</h2>
  <button v-on: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;
          },
        getClasses:function () {
          return {active:this.isactive,line:this.isLine}
        }
      }
    })

</script>
</body>
</html>

效果展示:
在这里插入图片描述
发现有点问题,于是就改了下,修改代码解决方法
上一篇文章
改后效果展示:
在这里插入图片描述
点击按钮并没有任何效果。

我改了下代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>

  <style>
    .active{
      color: red;
    }
  </style>
</head>
<body>
<div id="app">
  <!--<h2 class="active">{{message}}</h2>
  <h2 :class="active">{{message}}</h2>-->
  <!--<h2 v-bind:class="{key1:value1,key2:value2}">{{message}}</h2>-->
  <!--<h2 v-bind:class="{类名1:true,类名2:boolean}">{{message}}</h2>-->
  <h2 class="title" v-bind:class="{active:isactive,line:isLine}">{{message}}</h2>
  <button v-on: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;
          },
        getClasses:function () {
          return {active:this.isactive,line:this.isLine}
        }
      }
    })

</script>
</body>
</html>

最终效果如下:
在这里插入图片描述
点击后效果:
在这里插入图片描述
最后以上2种方案都可以。嘿嘿嘿!

  • 6
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值