ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## class的对象绑定 ``` <style> .activated { color: red; } </style> <body> <div id="app"> <div @click="change" :class="{ activated : isActivated}" > hello </div> </div> <script> var vm = new Vue({ el: '#app', data: { isActivated: false, }, methods: { change: function () { this.isActivated = !this.isActivated; } } }) </script> ``` ## 另一种方式 ``` <style> .activated { color: red; } </style> <body> <div id="app"> <div @click="change" :class="[activated]" > hello </div> </div> <script> var vm = new Vue({ el: '#app', data: { activated: '', }, methods: { change: function () { this.activated = this.activated === 'activated' ? '' : 'activated'; } } }) </script> ``` ## style内联样式 这种方式也可以通过数组的方式. ``` <body> <div id="app"> <div :style="styleObj" @click="change" > hello </div> </div> <script> var vm = new Vue({ el: '#app', data: { styleObj: { color: 'black' }, }, methods: { change: function () { this.styleObj.color = this.styleObj.color === 'black' ? 'red' : 'black'; } } }) </script> ```