) {
this
.
isActive
= !
this
.
isActive
;
console
.
log
(
this
.
color
);
this
.
color
===
'red'
? (
this
.
color
=
'blue'
) : (
this
.
color
=
'red'
);
.
red
{
color
: red; }
.
blue
{
color
: blue; }
三、用户输入颜色,动态改变字体颜色
<li class="liMenu" :class="idx === index ? color : ''"
@click="son(item, idx)"
v-for="(item, idx) in items" :key="idx">
{{ item }}
<input type="text" v-model="color" />
data() {
return {
items: ['第一个', '第二个', '第三个'],
index: 0,
color: 'red',
methods: {
son(item, idx) {
this.index = idx;
console.log(this.color);
.red { color: red; }
.blue { color: blue; }
实现效果:当用户输入red时,点击的li变成红色;用户输入blue时,点击的li变成蓝色
![image.png](https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/7652caab7d514458b3721a78147c4c38~tplv-k3u1fbpfcp-zoom-in-crop-mark:3024:0:0:0.image?)