zoukankan      html  css  js  c++  java
  • (尚007)Vue强制绑定class和style

    注意:class和style的值是动态的值

    1.test007.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
    .aClass{
    color:red;
    }
    .bClass{
    color:blue;
    }
    .cClass{
    font-size: 30px;
    }
    </style>
    </head>
    <body>
    <!--
    1.理解
    在应用界面中,某个(些)元素的样式是变化的
    class/style绑定就是专门用来实现动态样式效果的技术
    2.class绑定: class='xxx'
    xxx是字符串
    xxx是对象
    xxx是数组
    3.style绑定
    :style="{color:activeColor,fontSize:fontSize+'px'}"
    其中activeColor/fontSize是data属性
    -->
    <div id="demo">
    <h2>1.class绑定: :class='xxx'</h2>
    <p class="cClass" :class="a">xxx是字符串</p>
    <!--属性名是类名,值是布尔值;true类名留下来,false类名不留-->
    <!--<p :class="{aClass:true,bClass:false}">xxx是对象</p>-->
    <!--:class="{aClass:isA,bClass:isB}"动态绑定-->
    <p :class="{aClass:isA,bClass:isB}">xxx是对象</p>
    <!--[你想要的类名]-->
    <p :class="['aClass','cClass']">xxx是数组</p>

    <h2>2.style绑定</h2>
    <!--属性值是键值对-->
    <p :style="{color:activeColor,fontSize:fontSize+'px'}">:style="{color:activeColor,fontSize:fontSize+'px'}"</p>

    <button @click="update">更新</button>

    </div>
    <script text="text/javascript" src="../js/vue.js"></script>
    <script>
    new Vue({
    el: '#demo',
    data:{
    a:'aClass',
    isA:true,
    isB:false,
    activeColor:'red',
    fontSize : 20
    },
    methods:{
    update(){
    this.a='bClass',
    this.isA=false,
    this.isB=true,
    this.activeColor='green',
    this.fontSize=30
    }
    }
    })
    </script>
    </body>
    </html>
    2.浏览器打开

     点击更新按钮后:

     厉害了!!!

  • 相关阅读:
    利用nginx实现负载均衡和动静分离
    Nginx动静分离实现
    php中session 入库的实现
    php文字水印和php图片水印实现代码(二种加水印方法)
    采集图片水印添加
    [安全]PHP能引起安全的函数
    [安全]服务器安全之 PHP权限目录
    Centos下安装git的web服务器
    Centos下抓包
    UVA 10795
  • 原文地址:https://www.cnblogs.com/curedfisher/p/12014500.html
Copyright © 2011-2022 走看看