我有这个表,ids是例如:r0c1 =第0行第1列 我想填充这个脚本,但出了点问题(没有做任何事情):
var data = new Array();
var data[0] = new Array("999", "220", "440", "840", "1 300", "1 580", "2 050", "2 860", "4 800");
var data[1] = new Array("009", "999", "999", "999", "999", "999", "999", "999", "999");
for (i = 0; i < data.length; i++) {
for (j = 0; j < data[i].length; j++) {
document.getElementById("r" + i + "c" + j).innerHTML = data[i][j];
}
}
<table>
<tr>
<td id="r0c0" width=60px></td>
<td id="r0c1" width=60px></td>
<td id="r0c2" width=60px></td>
<td id="r0c3" width=60px></td>
<td id="r0c4" width=60px></td>
<td id="r0c5" width=60px></td>
<td id="r0c6" width=60px></td>
</tr>
<tr>
<td id="r1c0" width=60px></td>
<td id="r1c1" width=60px></td>
<td id="r1c2" width=60px></td>
<td id="r1c3" width=60px></td>
<td id="r1c4" width=60px></td>
<td id="r1c5" width=60px></td>
<td id="r1c6" width=60px></td>
</tr>
</table>
答案 0 :(得分:0)
删除var
和data[0]
的变量定义data[1]
。出现错误是因为您重新定义了变量data
var data = new Array();
data[0] = new Array("999", "220", "440", "840", "1 300", "1 580", "2 050", "2 860", "4 800");
data[1] = new Array("009", "999", "999", "999", "999", "999", "999", "999", "999");
for (i = 0; i < data.length; i++) {
for (j = 0; j < data[i].length; j++) {
document.getElementById("r" + i + "c" + j).innerHTML = data[i][j];
}
}
<table>
<tr>
<td id="r0c0" width=60px></td>
<td id="r0c1" width=60px></td>
<td id="r0c2" width=60px></td>
<td id="r0c3" width=60px></td>
<td id="r0c4" width=60px></td>
<td id="r0c5" width=60px></td>
<td id="r0c6" width=60px></td>
</tr>
<tr>
<td id="r1c0" width=60px></td>
<td id="r1c1" width=60px></td>
<td id="r1c2" width=60px></td>
<td id="r1c3" width=60px></td>
<td id="r1c4" width=60px></td>
<td id="r1c5" width=60px></td>
<td id="r1c6" width=60px></td>
</tr>
</table>
答案 1 :(得分:0)
你有几个问题:
1)你不必在data[0]
和data[1]
前面使用'var',因为它们在数组中变成内容,而不是变量。如果你想要推送到数组,你只需要使用var。
2)我和j是全球性的,不要忘记他们的var。 (循环中的var i和var j)
3)如果你使用这种技术,你必须确保你的html包含与数组中元素一样多的单元格。你的数组包含9个元素,但每个html行只有6个单元格,所以你的脚本停止工作iocne它试图找到第一行的第7个单元格。
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.gone {
display: none;
}
</style>
</head>
<body>
<table>
<tr>
<td id="r0c0" width=60px></td>
<td id="r0c1" width=60px></td>
<td id="r0c2" width=60px></td>
<td id="r0c3" width=60px></td>
<td id="r0c4" width=60px></td>
<td id="r0c5" width=60px></td>
<td id="r0c6" width=60px></td>
<td id="r0c7" width=60px></td>
<td id="r0c8" width=60px></td>
<td id="r0c9" width=60px></td>
</tr>
<tr>
<td id="r1c0" width=60px></td>
<td id="r1c1" width=60px></td>
<td id="r1c2" width=60px></td>
<td id="r1c3" width=60px></td>
<td id="r1c4" width=60px></td>
<td id="r1c5" width=60px></td>
<td id="r1c6" width=60px></td>
<td id="r1c7" width=60px></td>
<td id="r1c8" width=60px></td>
<td id="r1c9" width=60px></td>
</tr>
</table>
<script>
var data = new Array();
data[0] = new Array("999" , "220" , "440" , "840" , "1 300" , "1 580" , "2 050" , "2 860" , "4 800");
data[1] = new Array("009" , "999" , "999" , "999" , "999" , "999" , "999" , "999" , "999");
for (var i = 0; i < data.length; i++) {
for (var j = 0; j < data[i].length; j++) {
document.getElementById("r" + i + "c" + j).innerHTML = data[i][j];
}
}
</script>
</body>
</html>
旁注:不需要使用新的Array()来创建数组,最好是使用[]或者只是完全写出数组。
var data = [];
data[0] = ["999" , "220" , "440" , "840" , "1 300" , "1 580" , "2 050" , "2 860" , "4 800"];
data[1] = ["009" , "999" , "999" , "999" , "999" , "999" , "999" , "999" , "999"];
甚至更好:
var data = [
["999" , "220" , "440" , "840" , "1 300" , "1 580" , "2 050" , "2 860" , "4 800"],
["009" , "999" , "999" , "999" , "999" , "999" , "999" , "999" , "999"]
];