【发布时间】:2020-08-20 10:11:49
【问题描述】:
window.onload = choosePic;
var myPix = new Array("img1.jpg", "img2.jpg", "img3.jpg");
function choosePic() {
var randomNum = Math.floor(Math.random() * myPix.length);
document.getElementById("myPicture").src = myPix[randomNum];
document.getElementById("myPicture2").src = myPix[randomNum];
document.getElementById("myPicture3").src = myPix[randomNum];
}
<img src="img.jpg" width="100" height="100" id="myPicture" alt="some image" />
<img src="img.jpg" width="100" height="100" id="myPicture2" alt="some image" />
<img src="img.jpg" width="100" height="100" id="myPicture3" alt="some image" />
<button id="btn" onclick="choosePic()">Click Hear</button>
我想从数组中显示 3 个不同的图像。并在第 4 次或第 5 次单击想要显示相同的图片。
【问题讨论】:
-
same pic表示前 3 个中的哪一个? -
有时应该显示 img1.jpg,有时是 img2.jpg,有时是 img3.jpg @brk
标签: javascript arrays image button random