实现效果如下图所示:
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.active{
color: red;
}
</style>
</head>
<body>
<div id="app">
<ul>
<li v-for="(item,index) in movies"
:class="{active:currentIndex === index}"
@click="liClick(index)">{{item}}</li>
</ul>
</div>
<script src="../js/vue.js"></script>
<script>
const app = new Vue({
el:'#app',
data:{
movies:['钢铁侠','绿巨人','美国队长','雷神','复仇者联盟'],
currentIndex:0,//记录当前显示为红色的标签
isActive:true
},
methods:{
liClick(index){
this.currentIndex = index;
console.log(index);
}
}
})
</script>
</body>
</html>