Vue3 v-for点击切换样式

在div上绑定  :class= { 名称(class/id):a === key 值 }  并添加一个点击事件

声明const  a  = ref(0);  css写我们要的样式(就是我们绑定的class名称)

点击事件里写   i.value = key

直接来代码:

<template>
  <div>
    <p
      v-for="arr in arr"
      :key="arr.id"
      :class="{ blue: i === arr.id }"
      @click="aa(arr.id)"
    >
      {{ arr.name }}
    </p>
  </div>
</template>
 
<script>
import { ref } from "vue";
export default {
  setup() {
    const arr = ref([
      {
        id: 1,
        name: "张三",
      },
      {
        id: 2,
        name: "李四",
      },
      {
        id: 3,
        name: "王五",
      },
    ]);
    const i = ref(1);
 
    const aa = (arr) => {
      i.value = arr;
    };
    return {
      arr,
      aa,
      i,
    };
  },
};
</script>
<style>
.blue {
  color: #2979ff;
}
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值