目标是通过将变量“id”设为随机数来随机选择图像。问题是如何在使某些东西消失并添加随机图像的函数上使用它,以及如何在“checkAnswer()”函数中使用它。
这段代码对吗?
var cobras=new Array();
cobras[0] = '<img src="cobra1.jpg">';
cobras[1] = '<img src="cobra2.jpg">';
cobras[2] = '<img src="cobra3.jpg">';
cobras[3] = '<img src="cobra4.jpg">';
cobras[4] = '<img src="cobra5.jpg">';
cobras[5] = '<img src="cobra6.jpg">';
cobras[6] = '<img src="cobra7.jpg">';
cobras[7] = '<img src="cobra8.jpg">';
cobras[8] = '<img src="cobra9.jpg">';
cobras[9] = '<img src="cobra10.jpg">';
cobras[10] = '<img src="cobra11.jpg">';
id=Math.floor((Math.random()*10)+1);
function makeDisappear() {
var elem = document.getElementById("main");
elem.style.visibility = "hidden";
var elem = document.getElementById("empty");
elem.style.visibility = "visible";
var bodyE1 = document.body;
bodyE1.innerHTML += cobras[id];
}
function checkAnswer(a) {
if (a==id) {
alert('Correct!')
}
else {
alert('Wrong!')
}
}
这就是我所说的checkAnswer
:
<area shape="rect" coords="0,230,190,40" alt="1" onclick="checkAnswer('1')">