<!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>
<!-- 第一个值默认有一个index 和默认设置的currentIndex一样 -->
<li v-for="(item,index) in movies" :class="{active:currentIndex===index}" @click="liClick(index)">{{item}}</li>
</ul>
</div>
<script src='../vue.js'></script>
<script>
const app = new Vue({
el: '#app',
data: {
movies: ['海贼王', '海尔兄弟', '海贼王', '海的女儿'],
// 记录现在选中的是第几个
currentIndex: 0
},
methods: {
liClick(index) {
this.currentIndex = index
}
}
})
</script>
</body>
</html>
|