- :class=“变量”
- :class=“三元运算符”
- :class=“{类名1:ture,类名2:false,类名3:false}”
当样式超过2个以上显示时,需要使用{},类名的值为true时,样式显示,为false时隐藏
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./vue.js"></script>
<style>
.red{
background: red;
}
.lime{
color: lime;
}
.blue{
background: blue;
}
.grey{
background: grey;
}
</style>
</head>
<body>
<div id="app">
<h1 :class="classN">蒹葭苍苍</h1>
<button @click="changeBlue()">变蓝</button>
<button @click="changeGrey()">变灰</button>
<hr>
<h2 :class="isShow ? 'blue lime' : 'grey lime'">白露为霜</h2>
<ul>
<li v-for="(item,index) of arr" :class="index%2==0 ? 'blue lime' : 'grey lime'">{{item}}</li>
</ul>
<hr>
<div :class="{red:false,blue:true,lime:true}">小荷才露尖尖角</div>
<ul>
<li v-for="(item,index) in arr" :class="{red:index%3===0,grey:index%3===1,blue:index%3===2}">
{{item}}
</li>
</ul>
</div>
<script>
new Vue({
el:'#app',
data:{
classN:'red lime',
isShow:true,
arr:['科比','詹姆斯','乔丹','姚明','易建联','刘劲']
},
methods:{
changeBlue(){
this.classN = 'blue lime';
},
changeGrey(){
this.classN = 'grey lime';
}
}
})
</script>
</body>
</html>
<el-button type="primary" style="width: 100%" @click="submit" :loading="isRequesting">
{{ isRequesting ? '创建中' : '确定' }}
</el-button>