1 回答
TA贡献1796条经验 获得超4个赞
您可以根据状态的键绑定属性class并启用/禁用一个类。statebuttonsList
new Vue({
el: '#app',
data: {
buttonsList: [
{ genre: "Folk", state: false },
{ genre: "Rap", state: false },
{ genre: "Pop", state: false },
]
},
methods: {
getGenre (buttons, i) {
this.buttonsList[i].state = !this.buttonsList[i].state
}
}
})
.am-active {
background-color: rgb(21, 79, 202);
color: white;
}
.am-not-active {
background-color: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<ul>
<li>
<button
v-for="(buttons, i) in buttonsList" v-on:click="getGenre(buttons, i)"
:class="{
'am-active': buttons.state,
'am-not-active': !buttons.state
}"
:key="i"
>
{{ buttons.genre }}.
</button>
</li>
</ul>
</div>
- 1 回答
- 0 关注
- 133 浏览
添加回答
举报