微信小程序不允许DOM操作,所以不能用addClass这种方法,我的用法如下。
点击元素后,获取元素中data-select值,赋值给catalogSelect,然后判断二者是否相等,一样则添加class(class的css可以先写好)。
<view class="info_choose ">
<view class="catalog_name">花色view>
<view class="catalog_items display-flex-row" >
<block wx:for="{{catalogs}}">
<text data-select="{{item.select}}" class="{{item.select == catalogSelect ? 'active':''}} catalog_item" bindtap="chooseCatalog">{{item.catalogName}}text>
block>
view>
view>
Page({
data: {
catalogs:[
{ "catalogName": "卡其卡其",
"select":1
},
{
"catalogName": "其卡其卡卡其卡其",
"select": 2
},
{
"catalogName": "鲤鱼鲤鱼",
"select": 3
},
{
"catalogName": "神迹神迹卡其卡其",
"select": 4
},
],
catalogSelect:0,
},
chooseCatalog:function(data){
var that=this;
that.setData({
catalogSelect : data.currentTarget.dataset.select
})
}
})