微信小程序不允許DOM操作,所以不能用addClass這種方法,我的用法如下。
點(diǎn)擊元素后,獲取元素中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
})
}
})