Answer the question
In order to leave comments, you need to log in
How to change styles on a specific child element in vue?
Here is the code .
Now, when you click on any element, the styles of all elements change, but you need the color to change only for the child element whose parent we clicked.
Answer the question
In order to leave comments, you need to log in
If several elements can change their color at the same time:
data: () => ({
items: Array(5).fill(false),
}),
<div class="item" v-for="(n, i) in items">
<div class="parent" @click="$set(items, i, !n)">
<div class="child" :class="n ? 'green' : 'red'">{{ i + 1 }}</div>
</div>
</div>
data: () => ({
active: null,
}),
<div class="item" v-for="i in 5">
<div class="parent" @click="active = active === i ? null : i">
<div class="child" :class="active === i ? 'green' : 'red'">{{ i }}</div>
</div>
</div>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question