• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
壮哉我大狮子
博客园    首页    新随笔    联系   管理    订阅  订阅

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

<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) {

      var that = this;
      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>

posted @ 2018-01-26 14:53  壮哉我大狮子  阅读(459)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3