我创造了像candycrush这样的游戏,其中只有三个糖果出现。过了一段时间,又有三个糖果出现在同一排。我正在使用图像作为糖果,我只是想将它们添加到同一行的下三个单元格中,直到该行充满了9个糖果。以下是我的代码:
function main(){
var array=new Array("i.jpg","ic.jpg","w.png","milk.png","o1.png","d.jpg","c.png","d.png","corn.png","p.jpg");
for(;ro<9;ro++){
for(;c<9;){
var cell= document.getElementById["t"].rows[ro].cells[c];
var r= Math.floor(Math.random()*10);
img.src = array[r];
img.style.height="45px";
img.style.width="35px";
cell.append(img);
if(c==3)
setInterval(function fun(){c++;}, 1000);
else{
c++;
}
}
}
}
有一系列图像。它只需随机拍摄一张图像并将其附加到该特定单元格中。但问题是图像没有附加在单元格中。 c和ro是全局变量,每个变量都初始化为0。 以下是table creation.creation的代码。我动态创建它
function createtable(){
var table=document.getElementById("t");
for(var i=0; i<9; i++)
{
var row=table.insertRow(i);
for(var j=0; j<9;j++){
var x=row.insertCell(j);
}
main();}
因此,只需在上面的代码中,插入三个图像后,它会等待一段时间,然后它应该将图像附加到下三个单元格中。但它甚至没有显示单个图像。
答案 0 :(得分:1)
https://jsfiddle.net/pcconsolidated/rsex31ob/
在我看来,将表附加到数组的最简单方法是通过设置innerHTML或appendChild来附加图像。
//var imageArray=["i.jpg","ic.jpg","w.png","milk.png","o1.png","d.jpg","c.png","d.png","corn.png","p.jpg"];
var tableObj=[];
var candyTable=document.getElementById("candyTable");
var imageArray=["http://www.dezineguide.com/wp-content/uploads/2012/10/Create-a-Piece-of-Vector-Candy-in-Adobe-Illustrator.jpg","http://thumbs.dreamstime.com/z/hard-candy-cough-drop-white-background-24033664.jpg","data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJx8fLT0tMTU3Ojo6Iys/RD84QzQ5OjcBCgoKDQwNGg8PGjclHyU3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3N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Ou7O/W9nHRV7fTLS3lupU0n9fvkdKpKXVl0AAWAsFeGCoAEACABAH/2Q=="];
for(i=0; i<9; i++){
candyTable.innerHTML+="<tr id='row"+i+"'></tr>";
for(j=0;j<9;j++){
document.getElementById("row"+i).innerHTML+="<td style='width:80px;height:80px;' id='r"+i+"c"+j+"' ></td>";
}
}
for(i=0; i<9; i++){
tableObj[i]=[];
for(j=0;j<9;j++){
tableObj[i][j]=document.getElementById("r"+i+"c"+j);
}
}
function getCandy(){
return imageArray[Math.floor(Math.random()*imageArray.length)];
}
function addCandy(targetRow, targetCol){
tableObj[targetRow][targetCol].innerHTML="<img src='"+getCandy()+"' style='width:65px;height:65px;'>";
}
addCandy(3,3);
addCandy(3,4);
addCandy(3,5);
&#13;
<table border=solid>
<tbody id="candyTable" >
</tbody>
</table>
&#13;
上面将从糖果阵列中随机选择一个糖果并将其插入所选的编号为[0-8] [0-8]的单元格作为[row] [column]。我确定你已经有了loginc来选择要插入的列和行,因为你特意询问插入图像!希望这是你想要的。