vue 2.0 点击添加class,同时删除同级class

<template>
  <div class="n-header">
    <ul class="title-wrapper">
      <li class="n-fliter">筛选:</li>
      <li v-for="(item,index) in titles" :key="item.id" class="n-title" @click="select(index)" :class="{'active': item.show?'active': ''}">
        <a href="#">
          <span v-show="item.show"></span>{{item.sort}}</a>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      titles: [
        { sort: "全部", show: true },
        { sort: "前端开发", show: false },
        { sort: "后端开发", show: false },
        { sort: "移动端开发", show: false },
        { sort: "云计算/大数据", show: false },
        { sort: "测试/运维", show: false },
        { sort: "其他", show: false }
      ]
    };
  },
  methods: {
    select(index) {
      for (let i = 0; i < this.titles.length; i++) {
        this.titles[i].show = false;
      }
      this.titles[index].show = true;
    }
  }
};
</script>

<style scoped>
.n-header {
  display: inline-block;
  text-align: left;
  margin: 15px 0;
}
.title-wrapper {
  width: 1080px;
  height: 2.1rem;
}
.title-wrapper li {
  display: inline-block;
  font-size: 0.7rem;
  line-height: 1.5rem;
  height: 1.5rem;
}
.title-wrapper li.active {
  background: rgb(6, 124, 89);
}
.title-wrapper li.active a {
  color: #fff;
}
.n-fliter {
  color: rgb(139, 138, 138);
}
.n-title {
  background: rgb(230, 227, 227);
  margin: 0 5px;
  padding: 0 10px;
  border-radius: 5px;
}
.n-title a {
  color: rgb(6, 124, 89);
  text-decoration: unset;
}
.n-title a:hover {
  color: #363636;
}
</style>

效果如下:

转载于:https://www.cnblogs.com/shengnan-2017/p/9046415.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值