微信小程序有个属性hover-class='active',是指当点击列表元素时当按下鼠标左键会显示active样式,但是鼠标离开样式就会复原.可以参考以下解决方案,直接上代码: 

wxml:

1
2
3
4
5
6
7
8
<view class="taga">
 <view class="tag-title">标签</view>
 <view class="tag-box">
 <view wx:for="{{taga}}" wx:key="id" wx:for-index="i">
 <view class="taga-item {{currentItem==item.id?'active-tag':''}}" data-id="{{item.id}}" bindtap="tagChoose">{{item.name}}</view>
 </view>
 </view>
 </view>

js文件:

1
2
3
4
5
6
7
8
9
10
11
tagChoose:function(options){
var that = this
var id = options.currentTarget.dataset.id;
console.log(id)
//设置当前样式
that.setData({
'currentItem':id
})
 
 
}

核心点:class=”taga-item {{dateCurrent==item.id?'active-tag':”}}”模板文件中使用三元运算符,通过dateCurrent指定当前item的id

 

相关文章:

  • 2021-12-10
  • 2021-11-05
  • 2021-04-08
  • 2021-09-19
  • 2021-04-26
  • 2021-12-26
  • 2021-04-29
  • 2021-12-31
猜你喜欢
  • 2021-06-11
  • 2021-04-09
  • 2021-12-17
  • 2022-12-23
  • 2021-09-25
  • 2021-12-29
  • 2021-05-10
相关资源
相似解决方案