[js高手之路] vue系列教程 - 绑定class与行间样式style(6)
一、绑定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)的更多相关文章
- [js高手之路] vue系列教程 - 绑定设置属性的多种方式(5)
一.设置属性的值: {{data中的数据}} window.onload = function () { var c = new Vue({ el : '#box', data : { url : ' ...
- [js高手之路] vue系列教程 - 事件专题(4)
本文主要讲解事件冒泡,事件绑定的简写,事件默认行为,按键码等一系列与事件相关的知识. 一.事件绑定的简写,@事件类型. 之前我的[js高手之路] vue系列教程 - vue的事件绑定与方法(2) 用 ...
- [js高手之路] vue系列教程 - vue的事件绑定与方法(2)
一.在vue中,绑定事件,用v-on:事件类型, 如绑定一个点击事件, 我们可以这样子做 window.onload = function () { var c = new Vue({ el : 'b ...
- [js高手之路] vue系列教程 - vue的基本用法与常见指令(1)
本系列课程选用vue的版本为1.0.21, 什么是vue? vue是由尤雨溪开发的一款基于MVVM的框架,M->模型,V->视图, 也就是说模型数据改变了,视图也跟着改变, 视图内容改变, ...
- [js高手之路] vue系列教程 - 实现留言板todolist(3)
通过前面两篇文章的的学习,我们掌握了vue的基本用法. 本文,就利用这些基础知识来实现一个留言板, 老外把他称之为todolist. 第一步.使用bootstrap做好布局 <!DOCTYPE ...
- [js高手之路] vue系列教程 - 组件定义与使用上部(7)
组件是vue框架比较核心的内容,那么什么是组件呢? 通俗点讲:组件是由一堆html, css, javascript组成的代码片段, 作用是为了实现模块的重用 组件的基本用法: <div id= ...
- [js高手之路] es6系列教程 - 对象功能扩展详解
第一:字面量对象的方法,支持缩写形式 //es6之前,这么写 var User = { name : 'ghostwu', showName : function(){ return this.nam ...
- [js高手之路] es6系列教程 - 迭代器,生成器,for...of,entries,values,keys等详解
接着上文[js高手之路] es6系列教程 - 迭代器与生成器详解继续. 在es6中引入了一个新的循环结构for ....of, 主要是用来循环可迭代的对象,那么什么是可迭代的对象呢? 可迭代的对象一般 ...
- [js高手之路] es6系列教程 - 迭代器与生成器详解
什么是迭代器? 迭代器是一种特殊对象,这种对象具有以下特点: 1,所有对象都有一个next方法 2,每次调用next方法,都会返回一个对象,该对象包含两个属性,一个是value, 表示下一个将要返回的 ...
随机推荐
- 倒水问题 (FillUVa 10603) 隐式图
题意:本题的题意是给你三个杯子,第一二个杯子是空的,第三个杯子装满水,要求是量出一定容量d升的水.若是得不到d升的水,那就让某一个杯子里面的水达到d',使得d'尽量接近d升. 解题思路:本题是给出初始 ...
- 深入浅出了解frame和bounds
frame frame的官方解释如下: The frame rectangle, which describes the view's location and size in its supervi ...
- 《Linux调优工具oprofile的演示分析》
根据CPU架构oprofile采样的触发有两种模式:1) NMI模式: 利用处理器的performance counter功能, 指定counter的类型type和累进数量count. 比如 type ...
- hdu 5635 LCP Array(BC第一题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5635 LCP Array Time Limit: 4000/2000 MS (Java/Others) ...
- HDU 3569 Imaginary Date 简单期望
推一下公式.就会发现是这个.. 由于设结果有x种方案.则每一个数字出现的概率都均等,然后和就是x*m 每种方案的概率是1/x 每一个数出现的概率都是1/n 所以每一个方案的和就是 sum/n *m # ...
- adb连接手机报错解决方案汇总(win7)
>>adb devices常见错误: >>解决方案汇总 检查端口是否占用:netstat -ano | findstr 5037 | findstr LISTENING 检 ...
- IO流(File类,IO流的分类,字节流和字符流,转换流,缓冲流,对象序列化)
1.File类 File类可以在程序中 操作文件和目录.File类是通过建立File类对象,在调用File类的对象来进行相关操作的. 示例: public class Demo01 { public ...
- Django项目创建02
Django项目创建(ubuntu环境) 1. 创建项目目录,我是在root下创建了一个workspace文件夹:mkdir workspace 然后cd到该目录下 命令:django-adm ...
- Javascript中的Microtask和Macrotask——从一道很少有人能答对的题目说起
首先我们来看一道题目,如下javascript代码,执行后会在控制台打印出什么内容? async function async1() { console.log('async1 start'); aw ...
- JSON Schema 校验实例
JSON Schema 简介 JSON Schema is a vocabulary that allows you to annotate and validate JSON documents. ...