Class和Style绑定

Vue的属性

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <script src="http://unpkg.com/vue/dist/vue.js"></script>

    <script type="text/javascript">
        window.onload = function(){
            var vm = new Vue({
                el:'#box',
                data:{
                    url:'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png',
                },
            });
        }
    </script>
</head>
<body>
    <div id="box">
        <img src="{{url}}">
        <img v-bind:src="url">
    </div>
</body>
</html>

src="{{url}}"这种方式图片不能显示
这里写图片描述

这证明:在Vue中,HTML属性有特有的写法。
另外,v-bind:src="url"可以简写成:src="url"

        <img :src="url" width="100px" height="100px">

宽度和高端等HTML属性是直接可以的,但是还是建议按照Vue的规范。

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <script src="http://unpkg.com/vue/dist/vue.js"></script>

    <script type="text/javascript">
        window.onload = function(){
            var vm = new Vue({
                el:'#box',
                data:{
                    url:'https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png',
                    w:'200px',
                    t:'图标标题'
                },
            });
        }
    </script>
</head>
<body> 
    <div id="box">
        <img :src="url" :width="w" :title="t">
    </div>
</body>
</html>

class和style

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <script src="http://unpkg.com/vue/dist/vue.js"></script>

    <script type="text/javascript">
        window.onload = function(){
            var vm = new Vue({
                el:'#box',
                data:{
                    red:'red', //值red是定义的样式名
                    b:'b',
                },
            });
        }
    </script>
    <style type="text/css">
        .red{color: red}
        .b{background: blue}
    </style>
</head>
<body> 
    <div id="box">
        <span v-bind:class="[red,b]">文字</span>
    </div>
</body>
</html>

这是数组的用法:v-bind:class="[red,b]"
绑定的数据是:

                data:{
                    red:'red', //值red是定义的样式名
                    b:'b',
                },

还可以传入一个对象,以动态的切换class。

        <span v-bind:class="{red:true}">文字</span> <!-- 样式生效 --> 
        <span v-bind:class="{red:false}">文字</span> <!-- 样式不生效 --> 
        <span v-bind:class="{red}">文字</span><!-- 默认是true -->

style

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <script src="http://unpkg.com/vue/dist/vue.js"></script>

    <script type="text/javascript">
        window.onload = function(){
            var vm = new Vue({
                el:'#box',
                data:{
                    red:'red', //值red是定义的样式名
                },
            });
        }
    </script>
    <style type="text/css">
    </style>
</head>
<body> 
    <div id="box">
        <strong :style="{color:red}">文字文字文字</strong>
    </div>
</body>
</html>

数组的形式

        <strong :style="[c,b]">文字文字文字</strong>
                data:{
                    c:{color:'red'},
                    b:{background:'blue'}
                },

类似这些就不多讲了,建议直接看文档:http://cn.vuejs.org/v2/guide/class-and-style.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值