微信 程序 點擊 列表 添加 去除 屬性 简体版
原文   原文鏈接

首先說一下場景:我所循環的數據是對象數組,設置了一個屬性看成標記,經過這個標記的值判斷是否給改元素添加樣式數組

wxml:this

<view>
      <view wx:for="{{list}}" wx:key="num" class="list" >
        <text bindtap='changColor'  data-index='{{index}}' class='{{item.check?"text-active":""}}' >| {{item.message}}</text>
      </view>
</view>

js:spa

/*
  這裏獲取list是一個數組對象
  tomorrow: [
    {
      thing: '吃飯',
      check: false
    },
    {
      thing: '睡覺',
      check: false
    },
    { 
      thing: '打豆豆',
      check: false
    }
  ]
*/
changColor: function (e) { let index = e.currentTarget.dataset.index let arrs = this.data.list; if (arrs[index].check == false) { arrs[index].check = true; } else { arrs[index].check = false; } this.setData({ list: arrs }) },

核心在於修改對象屬性check的值,而後在元素渲染是根據值進行樣式的添加code

相關文章
相關標籤/搜索
每日一句
    每一个你不满意的现在,都有一个你没有努力的曾经。
本站公眾號
   歡迎關注本站公眾號,獲取更多信息