微信小程序实现给循环列表添加点击样式

来源:互联网 发布:wind终端mac版 编辑:程序博客网 时间:2024/06/01 23:23

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

 <view class="tags">    <view class="tag-title">标签</view>    <view class="tag-box">    <view wx:for="{{tags}}" wx:key="id" wx:for-index="i">      <view class="tags-item {{currentItem==item.id?'active-tag':''}}" data-id="{{item.id}}" bindtap="tagChoose">{{item.name}}</view>      </view>    </view>  </view>sdf

js文件:

  tagChoose:function(options){    var that = this     var id = options.currentTarget.dataset.id;     console.log(id)    //设置当前样式    that.setData({      'currentItem':id    })  }

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

1 0