vue.js 动态设置样式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wa172126691/article/details/83015534

控制多个样式:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<style>
.text-danger {
	width: 100px;
	height: 100px;
	background: red;
}
.active {
	width: 100px;
	height: 100px;
	color: green;
}
</style>
</head>
<body>
<div id="app">
	<div v-bind:class="[isTest ? errorClass : '',isActive ? activeClass : '']">ceshi</div>
</div>

<script>
new Vue({
  el: '#app',
  data: {
    isActive: true,
	isTest:true,  
	activeClass: 'active',
    errorClass: 'text-danger'
  }
})
</script>
</body>
</html>

控制单个样式:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试</title>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<style>
.active {
	width: 100px;
	height: 100px;
	background: green;
}
.text-danger {
	background: red;
}
</style>
</head>
<body>
<div id="app">
  <div class="static"
     v-bind:class="isActive?'active':''">
  </div>
</div>

<script>
new Vue({
  el: '#app',
  data: {
    isActive: true,
	hasError: true
  }
})
</script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/wa172126691/article/details/83015534