1.在v-for下?点击当前元素 动态变化,其他同级元素不变
vue2代码
html
<div class="weui-mask" id="guige">
<div class="guigeBox">
<p class="guigeTitle">{{guigeName}}</p>
<p class="guigeP guigeP01">规格</p>
<div class="indexGuiGe">
<span v-for="value,index in guigeList" v-on:click="guige(index)"
<!-- on为样式 index为下标 当index等于点击后传进来的下标 返回为true了 就自
动改变 这个样式-->
v-bind:class="{on:index==guigeSpan}">{{value.guige_name}}</span>
</div>
<p class="guigeP">口味</p>
<div class="indexKouwie">
<span v-for="value,index in kouweiList" v-on:click="kouwei(index)"
v-bind:class="{on:index==kouweiSpan}">{{value.guige_name}}</span>
</div>
</div>
</div>
script
var guige=new Vue({
el: '#guige',
data:{
guigeSpan:"-1", //控制点亮状态 -1为默认不点亮
kouweiSpan:"-1", //控制点亮状态
},
methods:{
guige:function(index){ //当点击时候点亮,同级的span标签删除Class
this.guigeSpan = index;
},
kouwei:function(index){
this.kouweiSpan = index;
},
}
})
vue3也差不多
<div
v-for="item,index in array"
:key="item">
<span :class="{pitch_on:index==isSelect}">
<button
@click="changeColor(index)"> {{ item.dictLabel }}</button>
</span>
</div>
const isSelect = ref(0)
const changeColor = (index:number) => {
isSelect.value = index
}
.pitch_on{
background-color: orange;
color: rgb(214, 34, 34);
border:1px solid red;
}
|