vue点击更改背景颜色

<template>
  <div>
    <div
      v-for="(item, index) in nameoptions"
      :class="{ active: index == isActive }"
      @click="checkItem(index)"
      :key="item.name"
    >
      {{ item.name }}
    </div>
  </div>
</template>
 
<script>
export default {
  name: "",
  data() {
    return {
      isActive: 0,
      nameoptions: [
        {
          name: "1",
        },
        {
          name: "2",
        },
        {
          name: "3",
        },
        {
          name: "4",
        },
      ],
    };
  },
  methods: {
    checkItem(index) {
      // console.log(index);
      this.isActive = index;
    },
  },
};
</script>
 
<style>
.active {
  background-color: blue;
  color: #fff;
}
</style>

来源:https://blog.csdn.net/weixin_47384925/article/details/119783229

上一篇:八、事件处理————温开水的复习笔记


下一篇:vue 父组件 与 子组件 的相互调用