先看效果图
直接上代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript" src="js/vue.js"></script>
<style type="text/css">
.listBox{
display: flex;
margin: 50px;
}
.list {
border: 1px solid #BEC3CB;
padding: 11px 27px;
border-radius: 100px;
margin-right: 20px;
cursor: pointer;
}
.checked {
color: #FFFFFF;
background: #3377FF;
border: 1px solid #3377FF;
}
</style>
</head>
<body>
<div id="box">
<div class="listBox">
<div class="list" v-for="(item,index) in labelList" :key="index" :class="{checked:arr.includes(index)}"
@click="checkedBox(index)">{{item}}</div>
</div>
</div>
<script type="text/javascript">
new Vue({
el:'#box',
data(){
return{
labelList: ["张力", "陈鑫", "胥庆洲", "颜志刚"],
arr: [],
}
},
methods: {
checkedBox(i) {
if (this.arr.includes(i)) {
this.arr = this.arr.filter(function(ele) {
return ele != i;
});
} else {
this.arr.push(i);
}
},
}
})
</script>
</body>
</html>