微信小程序有個屬性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