03-动态绑定属性

01-v-bind基本使用

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

<div id="app">
  <img v-bind:src="imageUrl" alt="zz">
  <a :href="baiduUrl">百度一下</a>
</div>

<script src="../../js/vue.js"></script>
<script>
  let app = new Vue({
    el: '#app',
    data: {
      message: 'hello bingo',
      imageUrl: 'https://img14.360buyimg.com/n0/jfs/t1/117516/19/11234/321017/5ef9c08dE19765e41/f12e794d4ecf208b.jpg'
      ,baiduUrl: 'http://www.baidu.com'
    }
  })
</script>

</body>
</html>

02-v-bind动态绑定class属性(对象语法)

<!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="bingo" :class="{active: this.isActive, line:this.isLine}">{{message}}</h2>
  <h2 class="bingo" :class="getClass()">{{message}}</h2>
  <button @click="changeColor">变色</button>
</div>

<script src="../../js/vue.js"></script>
<script>
  let app = new Vue({
    el: '#app',
    data: {
      message: 'hello bingo',
      isActive: true,
      isLine: true
    },
    methods: {
      changeColor: function (){
        this.isActive = !this.isActive;
      },
      getClass: function (){
        return {active: this.isActive, line:this.isLine}
      }
    }
  })
</script>

</body>
</html>

03-v-bind动态绑定class属性(数组语法)

<!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="[isActive, isLine]">{{message}}</h2><!--数组语法是写死的-->
  <h2 :class="getClass()">{{message}}</h2><!--数组语法是写死的-->
</div>

<script src="../../js/vue.js"></script>
<script>
  let app = new Vue({
    el: '#app',
    data: {
      message: 'hello bingo',
      isActive: 'aaa',
      isLine: 'bbb'
    },
    methods: {
      getClass: function (){
        return [this.isActive, this.isLine]
      }
    }
  })
</script>

</body>
</html>

04-v-bind和v-for结合作业

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    .active{
        color: red
    }
  </style>
</head>
<body>

<div id="app">
  <ul>
    <li v-for="(b,index) in movies" :class="getClass()" @click="changeColor">{{b}}</li>
  </ul>
</div>

<script src="../../js/vue.js"></script>
<script>
  let app = new Vue({
    el: '#app',
    data: {
      message: 'hello bingo',
      movies: ['大话西游','功夫','回魂夜','少林足球','喜剧之王','007'],
      active: false
    },
    methods: {
      changeColor: function (event){
        alert('我变成红色啦')
      },
      getClass: function (){
        return {active: this.active}
      }
    }
  })
</script>

</body>
</html>

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

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

<div id="app">
  <h2 :style="getStyles()">{{message}}</h2>
</div>

<script src="../../js/vue.js"></script>
<script>
  let app = new Vue({
    el: '#app',
    data: {
      message: 'hello bingo',
      fontSize: 100,
      fontColor: 'red'
    },
    methods: {
      getStyles: function (){
        return {fontSize: this.fontSize + 'px', color: this.fontColor}
      }
    }
  })
</script>

</body>
</html>

06-v-bind动态绑定style(数组语法)

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

<div id="app">
  <h2 :style="[baseStyle, upperStyle]">{{message}}</h2>
</div>

<script src="../../js/vue.js"></script>
<script>
  let app = new Vue({
    el: '#app',
    data: {
      message: 'hello bingo',
      baseStyle: {backgroundColor : 'red'},
      upperStyle: {fontSize: '200px'}
    }
  })
</script>

</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值