gpt4 book ai didi

javascript - 在 Titanium Appcelerator 中创建元素并使用 Loop 添加事件监听器

转载 作者:行者123 更新时间:2023-11-28 16:25:37 25 4
gpt4 key购买 nike

使用 Titanium Appcelerator 我尝试动态创建元素并使用循环向它们添加事件监听器。这是我当前的代码:

for(i=0;i<7;i++){

testLabels[i] = Titanium.UI.createLabel({
borderRadius: 35,
text:'hello',
textAlign:'center',
width:70,
height: 70,
top: '13%',
left:140,
touchEnabled: true
});

testLabels[i].addEventListener('click',function(e){
//do something
}
}

当我运行此程序时,出现以下错误:

Can't find variable: testLabels. 

对我来说有趣的是,它找不到的变量不是“testLabels1”,这对我来说意味着循环没有触发......有什么想法吗?

谢谢!

当我将“var”放在标签声明前面时,Titanium 不喜欢它。

最佳答案

试试这个

var testLabels = [];
for(var i=0; i<7; i++ ) {

testLabels[i] = Titanium.UI.createLabel({
borderRadius: 35,
text:'hello',
textAlign:'center',
width:70,
height: 70,
top: '13%',
left:140,
touchEnabled: true
});

(function(label) {
label.addEventListener('click',function(e){
//do something
}
}(testLabels[i]));

}

关于javascript - 在 Titanium Appcelerator 中创建元素并使用 Loop 添加事件监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8133819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com