<style>
.mydiv{
width:400px;
height:50px;
background-color: gray;
}
.red{
background-color: red;
}
.yellow{
background-color: yellow;
}
.green{
background-color: green;
}
.mywidth{
width: 450px;
}
</style>
<body>
<div id="app">
<div v-bind:class="{red:temp}"class="mydiv"></div>
<button type="button" @click="temp=!temp">点我</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script
<script>
var v1 = new Vue({
el:"#app",
data:{
temp:false
}
})
</script>
</body>
<body>
<div id="app">
<div :class="myClassStyle" class="mydiv"></div>
<button type="button" @click="temp=!temp">点我</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var v1 = new Vue({
el:"#app",
data:{
temp:false
},
computed:{
myClassStyle:function(){
return{
red:this.temp,
mywidth:this.temp
}
}
}
})
</script>
</body>
<body>
<div id="app">
<div :class="mycolor" class="mydiv"></div>
<button type="button" @click="temp=!temp">点我</button>
<!-- 绑定数据mycolor -->
<input type="text" v-model="mycolor" />
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var v1 = new Vue({
el:"#app",
data:{
temp:false,
mycolor:'green'
},
computed:{
myClassStyle:function(){
return{
red:this.temp,
mywidth:this.temp
}
}
}
})
</script>
</body>
<body>
<div id="app">
<div :class="[mycolor,mv]" class="mydiv"></div>
<button type="button" @click="temp=!temp">点我</button>
<input type="text" v-model="mycolor" />
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var v1 = new Vue({
el:"#app",
data:{
temp:false,
mycolor:'green',
mv:"mywidth"
},
computed:{
myClassStyle:function(){
return{
red:this.temp,
mywidth:this.temp
}
}
}
})
</script>
</body>
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有