Vue.js基础学习(三) -------------动态绑定v-bind的介绍和使用

Vue.js基础学习(三) -------------动态绑定v-bind的介绍和使用

一、v-bind介绍

v-bind:需要动态决定标签的属性的时候 可以使用到v-bind
语法糖的写法: : ,把v-bind替换为

二、v-bind的基本使用

动态绑定a标签的href属性,动态绑定img的src属性

    <img :src='imgURL'  >
    <a :href="aHref">百度一下</a>
     aHref:'http://www.baidu.com',
     imgURL:'/img/=.jpg'

三、v-bind动态绑定class

  <style>
    .Color{
      color:green;
    }
    .acl{
      color:red;
    }
  </style>
</head>
<body>
  <div id="app">
    <!--<h2 :class="{类名1:Boolean,类名2:Booolean}">{{message}}</h2>--> 
    <h2 :class="{ Color:isColor,  Line:isLine}">{{message}}</h2> <!--对象语法-->
    <h2 :class="getcolores()">{{message}}</h2>
    <button @click='color'>更换</button>
  </div>
  <script src="/vue.js"></script>
  <script>
    const app = new Vue({
      el:'#app',
      data:{
        message:'动态绑定class',
        isColor:true,
        isLine:true
      },
      methods: {
        color:function(){
          this.isColor =! this.isColor;
        },
        getcolores:function(){
          return{Color: this.isColor}
        }
      }
    })
  </script>
</body>

在绑定class的时候在类后面赋值一个Boolean值,这样可以决定它是否显示这个class属性

四、v-bind动态绑定style

body>
  <div id="app">
    <h3 :style="{fontSize:fonzise,color:Fcolor}">{{message}}</h3>
    <h3 :style="[fz,Fcolor]" >{{message}}</h3>
  </div>
  <script src="/vue.js"></script>
  <script>
    const app = new Vue({
      el:"#app",
      data:{
        message:'动态绑定style',
        fonzise:'100px',
        Fcolor:'red',
      },
    })
  </script>
</body>

在v-bind里绑定一个元素的style属性的时候,给style赋值一个对象,对象的名字是属性名,值是在vue实例的data里定义的,以方便动态的改变它的值

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值