damdmen
damdmen
  • 发布:2021-07-07 10:35
  • 更新:2021-09-14 09:39
  • 阅读:418

如何获取子元素&v-for生成的列表样式无法动态更新

分类:uni-app
<view v-if="allPairList.length > 0" v-for="(pair,index) in allPairList" :key="index" class="item">  
    <view class="row1">  
    <button type="" :data-index="index" @click="switch($event,pair.inWatch)">  
                  <uni-icons type="star-filled" :class="{active: pair.inWatch}" :color="pair.inWatch?'#ffaa00':'#999'" size="25" />   
         </button>  
    </view>  
</view>  

<script>  
data(){  
     return{  
           allPairList :[{  
                  name: '123',  
                  inWatch : false    
            }]  
     }  
}  
methods:{  
        switch(e, inWatch){  
        let elIndex = e.currentTarget.dataset.index  
            this.allPairList[elIndex].inWatch = ! this.allPairList[elIndex].inWatch     // 用这种方法可以改变inWatch属性的值,但icon的样式没变   
                e.currentTarget.firstElementChild.color = '#7e2c3a'             // 改用这种方法会报错,firstElementChild无法获取到uni-icon这个元素  
            console.log('点击了第'+elIndex+'个,pair = '+ this.allPairList[elIndex].inWathc)  
         }  
}  
</script>  

想要实现的是点击v-for生成的某个列表项之后,改变该列表项中icon的颜色(或icon类型)
试了两种方法都不太行,
第一种好像是因为v-for打印出的列表不支持动态更新class?
第二种不知道该怎么正确获取到被点击元素下的icon子元素,以及改变这个子元素的type或者class及color属性

请大侠们指点一二,谢谢

2021-07-07 10:35 负责人:无 分享
已邀请:
z***@163.com

z***@163.com

第一个问题:
加个样式,主要是 >>> 这个可以用于动态的样式

<style>  
    .row1 >>> .inWatch{ display:inline-block; width: 50%; text-align: center; font-size: 14px; padding: 8px 0px;}  
</style>

该问题目前已经被锁定, 无法添加新回复