html代码
<p id="box">
<strong>阿斯顿发</strong>
</p>css代码
.red {
color: red;
}
.gray {
background-color: gray;
}js代码
window.onload = function() {
new Vue({
el: '#box',
data: {
red: 'red',
gray: 'gray'
}
});
}样式生效的写法
:class="[red, gray]" 调用的是vue参数里的data属性
立即学习“前端免费学习笔记(深入)”;
<strong :class="[red, gray]"></strong>
:class="{red: true, gray: true}"
<strong :class="{red: true, gray: true}"></strong>第二种方法也是可以调用vue参数了的data属性的,记住,Vue里面一切皆是数据
new Vue({
el: '#box',
data: {
red: 'red',
gray: 'gray',
a: true,
b: false
}
});<strong :class="{red: a, gray: b}"></strong>:class="json",第二种方法的精简版,官方版本
new Vue({
el: '#box',
data: {
red: 'red',
gray: 'gray',
json: {
a: true,
b: false
}
}
});<strong :class="json"></strong>
和class基本相同
:style="{}"
<strong :style="{color: 'red', backgroundColor: 'gray'}"></strong>:style="a"
new Vue({
el: '#box',
data: {
red: 'red',
gray: 'gray',
a: {
color: 'red',
backgroundColor: 'gray' //注意复合样式的书写规范
}
}
});<strong :style="a">阿斯顿发</strong>
:style="[a, b]", a, b对应的是data里的两个json数据
更多vue中class和style设置的相关方法 相关文章请关注PHP中文网!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号