Vue实现点击li变色

一个小作业,v-bind和v-for结合实现点击li元素变色。
代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <style>
    .active{
     
      color: aqua;
    }
  </style>
</head>
<body>
<div id="app">
  <ul>
    <li v-for="(m,index) in movies"
        @click="btnClick(index)"
        :class="{active:currentIndex==index}">
      {
     {
     index}}-{
     {
     m}}
    </li>
  </ul>
</div>
<script src="../js/vue.js"></script>
<script>
  const app = new Vue({
     
    el: '#app',
    data: {
     
      currentIndex:0,
      movies: ['hello','zcc','world']
    },
    methods:{
     
      btnClick:function (index) {
     
        this.currentIndex=index;
      }
    }
  })
</script>
</body>
</html>

你可能感兴趣的:(vue,javascript)