v-bind/v-on/v-for结合练习

v-bind/v-on/v-for结合练习

鼠标点哪个电影其变为红色,默认第一个红色

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>作业</title>
  <style>
    .Red {
      color: red;
    }
  </style>
</head>
<body>
<div id="app">
  <ul>
<!-- 在v-bind:class中,true/false判断通过,当前点击的li的index和标志currentIndex是否一样来决定,默认情况下第一个亮-->
    <li v-for="(m,index) in movies" v-on:click="ulClick(index)" v-bind:class="{Red:index===currentIndex}">{{index}}-{{m}}</li>
  </ul>
</div>
<script src="../js/vue.js"></script>
<script>
  const app = new Vue({
    el: '#app',
    data: {
      movies: ['海贼王','海尔兄弟','火影忍者','进击的巨人'],
      //当前点的li,默认是第一个
      currentIndex:0,
    },
    methods:{
      ulClick:function (index) {
        //点哪个将当前的的下标赋值给currentIndex
        this.currentIndex = index;
      }
    }
  });
</script>
</body>
</html>

在这里插入图片描述

注意点

1.v-for=(item, index) in items不仅拿到了需要遍历的元素,其下标的索引也得到了。

2.ulClick函数传入这个index作为参数,设置一个标志currentIndex用来显示第几个li需要变色。默认情况是第一个为红色。

3.每次点击,将标志修改为下标,然后class属性里的布尔值,就可以通过index===currentIndex来判断,点哪一个其li中的class就添加Red样式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值