• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
黄洪波写点东西的地方
博客园    首页    新随笔    联系   管理    订阅  订阅
vue2.0 实现click点击当前li,动态切换class(转)

 

https://www.cnblogs.com/sxz2008/p/7048671.html

1,文件内容

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

 

<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,效果

posted on 2021-12-07 21:28  红无酒伤  阅读(242)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3