微信小程序 多個樣式 選中改變顏色 ,點擊本身改變顏色,在點擊本身取消顏色


 

 

 

wx.ml

    <view class="single_word {{item.show?'word_active':'word_noactive'}}" wx:for="{{arr}}" wx:key="{{this}}"  data-index="{{index}}" bindtap="wordShow">
                {{item.word}}
                <text class="c_sign" wx:if="{{item.show}}"></text>
            </view

 

wx.js

 data: {
    words: ["我", "是", "中", "國", "人",, "教", "育", "機", "構"],
    //前端處理的后端數據
    arr: null,
  },


/**
   * 生命周期函數--監聽頁面加載
   */
  onLoad: function (options) {
    let arr = new Array();
    let obj = {};
    this.data.words.forEach((item) => {
      obj.word = item;
      obj.show = false;
      arr.push(obj);
      obj = {};
    });
    this.setData(
      {
        arr,
      },
      () => {
        console.log(this.data.arr);
      }
    );
  },

  //點擊切換樣式
  wordShow(e) {
    // console.log(e.currentTarget.dataset.index)
    let idx = e.currentTarget.dataset.index;
    let arr = [...this.data.arr];
    let obj = {};
    arr.forEach((item, index) => {
      if (idx === index) {
        obj.word = item.word;
        obj.show = !item.show;
        arr.splice(index, 1, obj);
        console.log(obj);
        obj = {};
      }
    });
    this.setData({
      arr
    })
  },

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM