0

我想更改多维数组的值,但这段代码一直告诉我“无法设置未定义的属性'5'(我猜是数组的长度)”

obj.figures = new Array(
new Array(6,5,5,5,6,5,5),
new Array(5,0,0,0,5,0,0),
new Array(5,5,5,5,5,5,5),
new Array(0,1,1,1,0,2,2),
new Array(0,1,0,1,0,2,0)
);

function change() {
for(var y = 0; y<obj.figures.length; y++) {
    for(var x = 0; x<obj.figures[y].length; x++) {
        $(y+","+x).addEventListener("click", function() {
            console.log(this.id);
            obj.figures[y][x] = 5;
            }, false);
        }
    }
}
4

1 回答 1

0

您必须在此语句中切换 y 和 x 值:

obj.figures[y][x] = 5;

应该

obj.figures[x][y] = 5;
于 2013-06-05T11:50:12.543 回答