【发布时间】:2017-10-25 16:56:03
【问题描述】:
我在屏幕上有 384 个对象,我需要按特定顺序遍历它们,根据不断变化的变量更改它们的属性。以下是 .xml 文件中对象的样子:
<View formFactor="tablet" id="res1_1" class="resBit" left="1.5%" bottom="25.0%" />
<View formFactor="tablet" id="res1_2" class="resBit" left="1.5%" bottom="27.3%" />
<View formFactor="tablet" id="res1_3" class="resBit" left="1.5%" bottom="29.6%" />
<View formFactor="tablet" id="res1_4" class="resBit" left="1.5%" bottom="31.9%" />
[...]
<View formFactor="tablet" id="res16_22" class="resBit" left="93.0%" bottom="73.3%" />
<View formFactor="tablet" id="res16_23" class="resBit" left="93.0%" bottom="75.6%" />
<View formFactor="tablet" id="res16_24" class="resBit" left="93.0%" bottom="77.9%" />
这是 javascript 的样子:
// theValues rotates between a set of 100 or so such combinations
theValues = "2,2,3,4,5,5,4,3,2,2,3,4,5,5,4,3".split(",");
// Start on the left and move right
for (i=1; i<17; i++) {
// Start at the bottom and move up
for (ii=1; ii<25; ii++) {
if (ii < (theValues[i-1]) - 1) {
// Make first row solid
if (i == 1) { eval('$.res' + i + '_' + ii + '.setOpacity(1);'); }
// Paint reds
eval('$.res' + i + '_' + ii + '.setBackgroundColor("red");');
}
}
}
我得到的错误是:
[ERROR] : TiExceptionHandler: (main) [10567,152803] ----- Titanium Javascript Runtime Error -----
[ERROR] : TiExceptionHandler: (main) [0,152803] - In undefined:1,1
[ERROR] : TiExceptionHandler: (main) [0,152803] - Message: Uncaught ReferenceError: $ is not defined
[ERROR] : TiExceptionHandler: (main) [1,152804] - Source: $.res1_1.setOpacity(1);
如果我直接在代码中写$.res1_1.setOpacity(1);,它就可以工作。破坏它的是 eval 。想法?谢谢。
【问题讨论】:
-
有一段时间没有使用 appcelerator 但我认为你可以这样做
if (i == 1) { $['res' + i + '_' + ii].setOpacity(1); } -
成功了!请将其添加为答案,以便我接受。 :)
-
只是为评论点赞,没关系 :) 很高兴它奏效了。
标签: javascript loops runtime-error appcelerator titanium-alloy