1 回答
TA贡献1890条经验 获得超9个赞
直接上代码咯,其实选好被选中属性的数据结构一样可以解决问题
<div class="container">
<div v-for="(item, key) in attr">
<h2>{{item.attrName}}</h2>
<ul>
<li v-for="attrItem of item.attrList" v-bind:class="{active: actAttr[key].value == attrItem.attrVal}" @click="selectItem(key, attrItem.attrVal)">
{{attrItem.attrVal}}
</li>
</ul>
</div>
</div>
</template>
<script type="text/javascript">
export default {
data () {
return {
actAttr: [{
attrName: '颜色',
value: '红色'
}, {
attrName: '型号',
value: '中号'
}],
attr: [{
attrName: '颜色',
attrList: [{
attrVal: '紫色'
}, {
attrVal: '红色'
}]
}, {
attrName: '型号',
attrList: [{
attrVal: '大号'
}, {
attrVal: '中号'
}, {
attrVal: '小号'
}]
}]
}
},
methods: {
selectItem (key, val) {
console.log(val)
this.actAttr.splice(key, 1, {
attrName: this.attr[key].attrName,
value: val
})
console.log(this.actAttr[key])
}
}
}
</script>
<style type="text/css">
.container {
text-align: left;
}
.active {
color: red;
}
</style>
添加回答
举报