动态设置样式

时间:2025-01-22 12:07:28

控制多个样式:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例</title>
<script src="/vue/2.2.2/"></script>
<style>
.text-danger {
	width: 100px;
	height: 100px;
	background: red;
}
.active {
	width: 100px;
	height: 100px;
	color: green;
}
</style>
</head>
<body>
<div >
	<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="/vue/2.2.2/"></script>
<style>
.active {
	width: 100px;
	height: 100px;
	background: green;
}
.text-danger {
	background: red;
}
</style>
</head>
<body>
<div >
  <div class="static"
     v-bind:class="isActive?'active':''">
  </div>
</div>

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