vue踩坑之同时使用class切换选中状态和操作dom元素

这个博客展示了如何在Vue.js应用中使用`v-for`指令创建动态按钮组,并通过`ref`和`:class`实现按钮的状态管理和点击事件。文章详细解释了如何根据条件禁用按钮,以及使用对象形式的`class`来改变按钮的选中状态。同时,还提醒了`ref`操作DOM元素和`:class`绑定可能存在的执行顺序问题。
摘要由CSDN通过智能技术生成

<template>

   <div class="home">

   

    <div class="a1">

      <div  v-for="(item,index) in bhhi" :key="index">

        <el-button type="primary" @click="lop(index)" ref="lop">{{item}}</el-button>

      </div>

     

    </div>

    <div class="a2">

      <div   v-for="(item,index) in ki" :key="index">

        <el-button :class="{'act':index==indexx}" ref="akl" @click="lol(index)">{{item}}</el-button>

      </div>

      <!--

        注意::class="{'act':index==indexx}"和操作don元素会由一个先后顺序  会先执行class然后执行ref操作dom元素,所有这两个尽量分开来写

             上面是用对象形式的class来做了一个按钮选中状态

      -->

    </div>

     <div  v-for="(item,index) in ki" :key="index">

          <button :class="{'act':index==indexx}" @click="lolp(index)">{{item}}</button>

     </div>

  </div>

</template>

<script>

export default {

  name: 'HelloWorld',

  data(){

    return{

        bhhi:["jj","ff"],

        ki:['xx',"nn"],

        indexx:0

    }

  },

  created(){

   


 

  },

  methods:{

    lop(index){

          if(index==1){

            this.$refs.akl[1].disabled=true

          }else{

             this.$refs.akl[1].disabled=false

          }

    },

    lol(index){

      if(index==1){

            this.$refs.lop[1].disabled=true

          }else{

             this.$refs.lop[1].disabled=false

          }

           this.indexx=index

    },

     lolp(index){

    console.log(index)

      this.indexx=index

    }

   

  }

}

</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->

<style scoped>

.a1{

  display: flex;

   margin-left: 50px;

}

.a2{

  display: flex;

  margin-top: 50px;

  margin-left: 50px;

}

.act{

  background-color: red;

}

</style>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值