JavaScript在for循环中获取随机图像
所以我创建了一个for循环,它创建(在文本字段上确定)许多图像 例如,我在文本字段中键入“23”,然后单击按钮,屏幕上将随机显示23幅图像JavaScript在for循环中获取随机图像,javascript,arrays,for-loop,random,Javascript,Arrays,For Loop,Random,所以我创建了一个for循环,它创建(在文本字段上确定)许多图像 例如,我在文本字段中键入“23”,然后单击按钮,屏幕上将随机显示23幅图像 for(i=0; i < box2; i++) { this.y = Math.floor(Math.random() * 100) + 1; this.x = Math.floor(Math.random() * 100) + 1; this.img = document.createElement("img");
for(i=0; i < box2; i++) {
this.y = Math.floor(Math.random() * 100) + 1;
this.x = Math.floor(Math.random() * 100) + 1;
this.img = document.createElement("img");
this.img.src = sourceArray[num];
this.img.setAttribute("height", "100px");
this.img.style.position = "absolute";
this.img.style.top = this.y + "vh";
this.img.style.left = this.x + "vw";
this.img.addEventListener("click", Remove);
document.getElementById("bildausgabe").appendChild(this.img);
}
我如何更改它,使其随机显示所有3个图像
e、 g.我键入“12”,单击按钮,从给定的3个源中随机获得12张图像。您应该将
var num = Math.floor(Math.random() * 3);
循环中的行。您应该将
var num = Math.floor(Math.random() * 3);
循环中的线。哦,我犯了一个愚蠢的错误。现在正如期工作。非常感谢。哦,我犯了一个愚蠢的错误。现在正如期工作。非常感谢。