在这段代码中,我正在加载一些骰子图像,并试图在表格中显示一些。我不确定我是否正在构建正确的数组,但这是我发现使用图像传播数组的一个例子。
<table id="dicebox">
<tr>
<td id="d1"></td>
<td id="d2"></td>
<td id="d3"></td>
<td id="d4"></td>
<td id="d5"></td>
</tr>
</table>
<script>
var diceB = new Array(7);
//Blue dice
var diceP = new Array(7);
//Purple dice
var diceValue = new Array(0, 1, 2, 3, 4, 5, 6);
//initializing blue dice array
diceB[0] = new Image(50, 50);
diceB[0].src = "images/dice0B.gif"
diceB[1] = new Image(50, 50);
diceB[1].src = "images/dice1B.gif"
diceB[2] = new Image(50, 50);
diceB[2].src = "images/dice2B.gif"
diceB[3] = new Image(50, 50);
diceB[3].src = "images/dice3B.gif"
diceB[4] = new Image(50, 50);
diceB[4].src = "images/dice4B.gif"
diceB[5] = new Image(50, 50);
diceB[5].src = "images/dice5B.gif"
diceB[6] = new Image(50, 50);
diceB[6].src = "images/dice6B.gif"
for(var i = 1; i < 6; i++) {
document.getElementById('d' + i).innerHTML = diceB[i].src;
答案 0 :(得分:1)
如何使用appendChild
for(var i = 1; i < 6; i++) {
document.getElementById('d' + i).appendChild(diceB[i]);
答案 1 :(得分:1)
您没有编写图片代码,您必须执行以下操作:
for(var i = 1; i < 6; i++) {
document.getElementById('d' + i).innerHTML = '<img src="'+diceB[i].src+'" />';
}