页面加载如何调用点击事件
发布于 6 年前 作者 xia21 5839 次浏览 来自 问答

页面一加载想调用一个点击事件,苦思冥想不知道如何调用

9 回复

**

  * 生命周期函数–监听页面加载

  */

onLoad: function (options) {

console.log(“页面加载开始。。。。。。。。。。”)

changeColor();

// this.data.txtArray[1].changeColor=true

},

changeColor: function (res) {

var txtArray = [];

for (var i = 0; i < this.data.txtArray.length; i++) {

if (res.target.id == this.data.txtArray[i].id) {

txtArray[i] = { id: this.data.txtArray[i].id, changeColor: true, txt: this.data.txtArray[i].txt }

console.log(“ture”)

} else {

txtArray[i] = { id: this.data.txtArray[i].id, changeColor: false, txt: this.data.txtArray[i].txt }

console.log(“false”)

}

}

this.setData({

txtArray: txtArray

})

},

报错:

changeColor is not defined;at pages/login/homePage page lifeCycleMethod onLoad function

ReferenceError: changeColor is not defined

可以了, 谢谢! 有没有啥微信小程序交流群可以加的

点击事件背后不是执行的function吗?  你在onLoad事件里面手动执行function,效果不是一样吗?

143689755 QQ群

曾经我也想实现一个和这个一样的功能,但是并不能获取到当前点击的那个对象下的东西,所以你res.currentTarget

等等 都是没有用。

第三行:this.changeColor();

如何页面一加载 获取该点击元素的的整个对象

手动执行执行changeColor这个函数,他的参数btn是undefined,不要直接使用res.target.id,发现是undefined可以先给一个默认值

回到顶部