vue2.0中click点击当前li实现动态切换class

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

vue2.0中click点击当前li实现动态切换class

pearl007   2020-05-15 我要评论

1,文件内容

----//为item添加不存在的属性,需要使用vue提供的Vue.set( object, key, value )方法。  看详解:https://cn.vuejs.org/v2/api/

<template>
  <div>
    <ul>
      <li v-for="(item,$index) in items" @click="selectStyle (item, $index) " :class="{'active':item.active,'unactive':!item.active}" >
      {{item.select}} 
      <span class="icon" v-show="item.active">当我是图标</span>
      </li>
    </ul>
  </div>
</template>

<script>

  import Vue from 'vue'

  export default{
    data(){
      return {
        active: false,
        items: [
          {select:'第一行'},
          {select:'第二行'},
          {select:'第三行'},
          {select:'第四行'}
        ]
      }
    },


  methods: {
    selectStyle (item, index) {
      this.$nextTick(function () {
        this.items.forEach(function (item) {
          Vue.set(item,'active',false);
        });
        Vue.set(item,'active',true);
      });
    }
  }
 }
</script>

<!-- 样式 -->
<style>
  .active{
    color:red;
  }
  .unactive{
    color:#000;
  }
  .icon{
    float: right;
    font-size:12px;
  }


</style>

2,效果

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们