一、绑定class属性的方式

1、通过数组的方式,为元素绑定多个class

 <style>
.red {
color:red;
/*color:#ff8800;*/
}
.bg {
background: #000;
/*background: green;*/
}
</style> window.onload = function(){
var c = new Vue({
el : '#box',
data : {
red_color : 'red',
bg_color : 'bg'
}
});
} <div id="box">
<span :class="[red_color,bg_color]">this is a test string</span>
</div>

上例为span 绑定2个class,通过设定red_color和bg_color的值,找到对应的class类名

2、通过控制class的true或者false,来使用对应的class类名, true: 使用该class,  false: 不使用该class

     <style>
.red {
color:red;
}
.bg {
background: #000;
}
</style> window.onload = function(){
var c = new Vue({
el : '#box',
data : {
}
});
} <div id="box">
<span :class="{red:true,bg:true}">this is a test string</span>
</div>

3、这种方式,跟第二种差不多,只不过是把class的状态true/false用变量来代替

     <style>
.red {
color:red;
}
.bg {
background: #000;
}
</style> window.onload = function(){
var c = new Vue({
el : '#box',
data : {
r : true,
b : false
}
});
} <div id="box">
<span :class="{red:r,bg:b}">this is a test string</span>
</div>

4、为class属性绑定整个json对象

     <style>
.red {
color:red;
}
.bg {
background: #000;
}
</style> window.onload = function(){
var c = new Vue({
el : '#box',
data : {
json : {
red : true,
bg : false
}
}
});
} <div id="box">
<span :class="json">this is a test string</span>
</div>

 二、绑定style行间样式的多种方式

1、使用json格式,直接在行间写

1     window.onload = function(){
2 var c = new Vue({
3 el : '#box',
4 });
5 }
6
7 <div id="box">
8 <span :style="{color:'red',background:'#000'}">this is a test string</span>
9 </div>

2、把data中的对象,作为数组的某一项,绑定到style

 1     window.onload = function(){
2 var c = new Vue({
3 el : '#box',
4 data : {
5 c : {
6 color : 'green'
7 }
8 }
9 });
10 }
11
12 <div id="box">
13 <span :style="[c]">this is a test string</span>
14 </div>

3、跟上面的方式差不多,只不过是为数组设置了多个对象

 1     window.onload = function(){
2 var c = new Vue({
3 el : '#box',
4 data : {
5 c : {
6 color : 'green'
7 },
8 b : {
9 background : '#ff8800'
10 }
11 }
12 });
13 }
1     <div id="box">
2 <span :style="[c,b]">this is a test string</span>
3 </div>

4、直接把data中的某个对象,绑定到style

 1     window.onload = function(){
2 var c = new Vue({
3 el : '#box',
4 data : {
5 a : {
6 color:'yellow',
7 background : '#000'
8 }
9 }
10 });
11 }
1     <div id="box">
2 <span :style="a">this is a test string</span>
3 </div>

[js高手之路] vue系列教程 - 绑定class与行间样式style(6)的更多相关文章

  1. [js高手之路] vue系列教程 - 绑定设置属性的多种方式(5)

    一.设置属性的值: {{data中的数据}} window.onload = function () { var c = new Vue({ el : '#box', data : { url : ' ...

  2. [js高手之路] vue系列教程 - 事件专题(4)

    本文主要讲解事件冒泡,事件绑定的简写,事件默认行为,按键码等一系列与事件相关的知识. 一.事件绑定的简写,@事件类型.  之前我的[js高手之路] vue系列教程 - vue的事件绑定与方法(2) 用 ...

  3. [js高手之路] vue系列教程 - vue的事件绑定与方法(2)

    一.在vue中,绑定事件,用v-on:事件类型, 如绑定一个点击事件, 我们可以这样子做 window.onload = function () { var c = new Vue({ el : 'b ...

  4. [js高手之路] vue系列教程 - vue的基本用法与常见指令(1)

    本系列课程选用vue的版本为1.0.21, 什么是vue? vue是由尤雨溪开发的一款基于MVVM的框架,M->模型,V->视图, 也就是说模型数据改变了,视图也跟着改变, 视图内容改变, ...

  5. [js高手之路] vue系列教程 - 实现留言板todolist(3)

    通过前面两篇文章的的学习,我们掌握了vue的基本用法. 本文,就利用这些基础知识来实现一个留言板, 老外把他称之为todolist. 第一步.使用bootstrap做好布局 <!DOCTYPE ...

  6. [js高手之路] vue系列教程 - 组件定义与使用上部(7)

    组件是vue框架比较核心的内容,那么什么是组件呢? 通俗点讲:组件是由一堆html, css, javascript组成的代码片段, 作用是为了实现模块的重用 组件的基本用法: <div id= ...

  7. [js高手之路] es6系列教程 - 对象功能扩展详解

    第一:字面量对象的方法,支持缩写形式 //es6之前,这么写 var User = { name : 'ghostwu', showName : function(){ return this.nam ...

  8. [js高手之路] es6系列教程 - 迭代器,生成器,for...of,entries,values,keys等详解

    接着上文[js高手之路] es6系列教程 - 迭代器与生成器详解继续. 在es6中引入了一个新的循环结构for ....of, 主要是用来循环可迭代的对象,那么什么是可迭代的对象呢? 可迭代的对象一般 ...

  9. [js高手之路] es6系列教程 - 迭代器与生成器详解

    什么是迭代器? 迭代器是一种特殊对象,这种对象具有以下特点: 1,所有对象都有一个next方法 2,每次调用next方法,都会返回一个对象,该对象包含两个属性,一个是value, 表示下一个将要返回的 ...

随机推荐

  1. 递归回溯 UVa140 Bandwidth宽带

    本题题意:寻找一个排列,在此排序中,带宽的长度最小(带宽是指:任意一点v与其距离最远的且与v有边相连的顶点与v的距离的最大值),若有多个,按照字典序输出最小的哪一个. 解题思路: 方法一:由于题目说结 ...

  2. onunload事件和onbeforeunload事件

    记录知识点背景:在做一个h5项目时,在统计事件时有这样一个需求, 希望能统计到用户是从第几页离开的,用到了这个知识点.在此记录. window.onunload 1.定义和用法 onunload事件在 ...

  3. hdu 5305 friends

    每一次比赛的时候脑子都卡顿, 这次更离谱,我居然二进制枚举边,这么大的复杂度.而且剪不了枝 后来学长说着是道爆搜.搜每一条边.恍然大悟. 仅仅须要剪掉点的度数是奇数的时候,或者他的线上朋友或线下朋友大 ...

  4. Memory Monitor

    Heap Viewer,Memory Monitor和Allocation Tracker是用来可视化你的app使用内存的补充工具. 使用Memory Monitor Tool来发现是否有不好的内存回 ...

  5. 使用Logstash来实时同步MySQL数据到ES

    上篇讲到了ES和Head插件的环境搭建和配置,也简单模拟了数据作测试 本篇我们来实战从MYSQL里直接同步数据 一.首先下载和你的ES对应的logstash版本,本篇我们使用的都是6.1.1 下载后使 ...

  6. redis 报Operation against a key holding the wrong kind of value警告的解决方法

    WRONGTYPE Operation against a key holding the wrong kind of value github:https://github.com/antirez/ ...

  7. 自学Zabbix3.5.1-监控项item-key详解

    自学Zabbix3.5.1-监控项item-key详解个人觉得艰难理解,故附上原文档:https://www.zabbix.com/documentation/3.0/manual/config/it ...

  8. Tomcat在Linux服务器上的BIO、NIO、APR模式设置

    一.BIO.NIO.AIO 先了解四个概念: 同步 : 自己亲自出马持银行卡到银行取钱(使用同步IO时,Java自己处理IO读写). 异步 : 委托一小弟拿银行卡到银行取钱,然后给你(使用异步IO时, ...

  9. JSON--stringify() 和 parse() 方法

    序列化:stringify()将JavaScript对象序列号为JSON字符串反序列化:parse()将JSON字符串解析为原生JavaScript值 序列化选项:JSON.stringify()除了 ...

  10. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...