按照官方最新文檔循環的方式,索引值是以 wx:for-index="index" 方式寫的,
以 parseInt(event.currentTarget.dataset.index) 方式獲取索引一直是NaN,注意這里用的是currentTarget,而不是target。
這里說一下他們的區別:target指向發生事件的組件,currentTarget指向綁定事件的組件
1.currentTarget:綁定的事件當前元素及其子元素都會觸發
2.target: 綁定的事件 子元素不會被觸發事件
將索引方法改成 data-index="{{index}}"
index的用法就是 取出每個數組的下標
如:
data-index=“{{index}}” 就是取出wx:for="{{UpImgSrcIcon}}" UpImgSrcIcon這個數組里的每個下標。
當點擊事件運行時 e.currentTarget.dataset.index 就可以取到當前遍歷出來 點擊的數組的下標