小程序setData修改属性
data: { itemUrl:[ { name:'', url:'../../images/item1.png' }, { name: '', url: '../../images/item2.png' }, { name: '', url: '../../images/item3.png' }, { name: '', url: '../../images/item4.png' }, { name: '', url: '../../images/item5.png' }, { name: '', url: '../../images/item6.png' }, { name: '', url: '../../images/item7.png' }, { name: '', url: '../../images/item8.png' }, { name: '', url: '../../images/item9.png' } ] }, onLoad: function () { var wx = this; setInterval(function(){ var x = Math.floor(Math.random() * 8 + 1); var that = 'itemUrl[' + x + '].name';// 先用一个变量,把itemUrl.name用字符串拼接起来 for (var i = 0; i <wx.data.itemUrl.length; i++) {
var all = 'itemUrl[' + i + '].name'; wx.setData({ [all]: '' // 使用【】将字符串包起来,为其赋值 }) } wx.setData({ [that]: 'active' // 使用【】将字符串包起来,为其赋值 }) },3000) } })