[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, 表示下一个将要返回的 ...
随机推荐
- Java读取打印机自定义纸张.
打印出现截断? 对于自定义纸张打印, 一定要先在打印机配置那边添加, 不然会出现截断. 例如打印1000*500, 出来是正常的, 打印216*139, 出现了截断. 因为java默认的打印, 会从打 ...
- C++ stack
stack 栈,一种后进先出的数据结构,在c++ stl里作为容器适配器,string,vector,deque,在内存中是连续的 声明方式 stack<int,deque<T>&g ...
- POJ 2482 Stars in Your Window(线段树)
POJ 2482 Stars in Your Window 题目链接 题意:给定一些星星,每一个星星都有一个亮度.如今要用w * h的矩形去框星星,问最大能框的亮度是多少 思路:转化为扫描线的问题,每 ...
- day01_使用Android Studio创建第一个Android项目
使用Android Studio开发Android项目如此简单 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize ...
- hdu2993之斜率dp+二分查找
MAX Average Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 对使用多个swiper下标有时显示不出来的问题
这久写了一个网页,其中有很多的轮播图及tab页面切换,就使用了swiper框架,有时一个网页要用到6-8个,如此就出现了下图这种问题: 有时刷新看不到,有时又能看到,tab切换过去的页面也看不到,其实 ...
- junit初探
由于公司规模不大,所以测试方面一直不是很正规,都是做完一个功能,稍微测试一下,没有做单元测试,所以自然也没有接触过类似于junit这类测试的工具. 今天有空研究了一下junit,顾名思义,这是给jav ...
- github not authorized eclipse 关于 代码不能提交到GitHub
eclipse/myeclipse > menu > window > preferences > general > security > content > ...
- IntelliJ IDEA 设置Output (输出窗口)窗口字体大小
Settings-->Editor-->Colors&Fonts-->Console Font 如图: 字体调好了以后使用起来眼睛就轻松多了
- 使用angularjs实现注册表单
本文是在学习angularjs过程中做的相应的练习 github地址 https://github.com/2016Messi/angularjs1.6-form 演示地址 https://2016m ...