1.代码实现过程
代码如下:
<!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="changeColor(index)">{{index}}-{{item}}</li>
</ul>
</div>
<script src="https://vuejs.org/js/vue.js"></script>
<script>
const app = new Vue({
el: "#app",
data: {
movies: ['长津湖', '战狼2', '送你一朵小红花', '沙丘', '世上只有妈妈好'],
currentIndex: -1
},
methods: {
changeColor: function(index) {
this.currentIndex = index;
}
}
})
</script>
</body>
</html>