微信小程序不允许DOM操作,所以不能用addClass这种方法,我的用法如下。
<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>
//js代码 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 }) } }) |