【发布时间】:2013-12-03 16:52:13
【问题描述】:
我正在编写一个需要使用 Javascript 二维数组的程序,因此我构建了这个测试台来尝试将值添加到数组中。
如您所见,如果您检查输出,循环会运行两次内部循环然后停止,不强制执行外部循环运行 10 次的要求。
谁能解释我做错了什么?
HTML:
<body>
<input type="button" value="Press me!" id="pressMe" onclick="primaryCommand('textBox')">
<textarea id="textBox"></textarea>
</body>
Javascript:
function primaryCommand(input){
arrayTest(input);
}
function arrayTest(input){
// How large can an array be and still be safe?
var array = new Array(new Array());
var obj = document.getElementById(input);
obj.value="";
var x = 0, y = 0;
for (x = 0; x < 10; x++)
{
for (y = 0; y < 2; y++)
{
array[x][y] = "Hello World, x='" + x + "', y='" + y + "'\n";
obj.value+=array[x][y];
}
}
}
输出:
Hello World, x='0', y='0'
Hello World, x='0', y='1'
【问题讨论】:
标签: javascript for-loop multidimensional-array getelementbyid