
<!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">
<li v-for="(item,index) in movies" @click="change(index)" :class="getclass(index)">{
{index}}{
{item}}</li>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<script> const app = new Vue({
el: '#app', data: {
movies: ['海王','钢铁侠','蜘蛛侠','美国队长'], isActive: true, n: -1 }, methods: {
getclass: function(index) {
if(this.n === index) {
return {
active: this.isActive} } }, change: function(index) {
if(this.n === index) {
this.isActive = !this.isActive }else {
this.n = index } } } }) </script>
</body>
</html>