<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<style>
*{
margin: 0;
padding: 0;
}
#app {
padding: 20px;
box-sizing: border-box;
}
ul{
list-style: none;
display: flex;
display: -webkit-flex; /*Webkit内核的浏览器,必须加上-webkit前缀。 Safari */
}
ul li {
width: 30%;
background-color: lightgoldenrodyellow;
}
ul li.bgcolor{
background: lightgreen;
}
.db{
display: block;
text-align: center;
}
</style>
</head>
<body>
<div id="app">
<ul>
<li v-for="(item,i) in listData" :class="{'bgcolor':active==i}" :key="i" @click="changeTab(item,i)">
<span class="db">{{item.name}}</span>
<span class="db">{{item.sex}}</span>
<span class="db">{{item.age}}</span>
<span class="db">{{item.score}}</span>
</li>
</ul>
</div>
</body>
</html>
<script>
var app = new Vue({
el: '#app',
data: {
active: 1, //默认选中的索引
listData: [
{name: 'Lucky', sex: '女',age: '24', score: 90},
{name: 'Ane', sex: '男',age: '23', score: 70},
{name: 'Danny', sex: '男',age: '29', score: 90},
]
},
methods: {
changeTab(item,i){
this.active = i;
}
}
})
</script>
效果预览: