开发者

How can I fix this "Cannot create property text on String" error?

开发者 https://www.devze.com 2023-01-22 11:30 出处:网络
I have 3 dynamic texts (h1,h2,h3) but I can\'t seem to do this: var n:Array=[\"n1\",\"n2\",\"n3\"]; for(var i = 0;i < 3; i++){

I have 3 dynamic texts (h1,h2,h3) but I can't seem to do this:

var n:Array=["n1","n2","n3"];

for(var i = 0;i < 3; i++){
    n[开发者_运维百科i].text="hello";
}

This code gives me the error "Cannot create property text on String."


The reason you're getting this error is because the array n contains 3 strings (n1, n2 and n3). So when you say: n[i].text, you are trying to set a non-existent property on a string.

If h1, h2 and h3 are the instance names of your text boxes, in your loop use this instead:

this["h"+i+1].text = "hello";

This code will reference the h1, h2 and h3 text boxes now. The reason there is a +1 added to i is because you are starting at 0, yet your first text box has a 1.


A Fix:

this["t"+(i+1)].text = "j";
0

精彩评论

暂无评论...
验证码 换一张
取 消