微信小程式實現給迴圈列表新增點選樣式例項
阿新 • • 發佈:2019-02-14
微信小程式有個屬性hover-class='active',是指當點選列表元素時當按下滑鼠左鍵會顯示active樣式,但是滑鼠離開樣式就會復原.可以參考以下解決方案,直接上程式碼:
wxml:
<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檔案:
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