-2
這是我到目前爲止。 newNumber
是我爲了做最小和最大而做的功能。我真的遇到了這個問題。如何使用開關和外殼生成圖片?
function onclick() {
var num = parseFloat(document.getElementById("do").value);
if (num <= 0) {
alert("Please enter a bet amount between 1 and 100");
}
else if (num > 100) {
alert("Please enter a bet amount between 1 and 100");
}
var stake = document.getElementById("txtStake").value;
if (stake < num) {
alert("Wrong number Can't Be Higher Than Stake Number")
}
var random = newNumber(1, 7);
if (random == 1) {
document.getElementById("toChangeColor").style.backgroundColor = "red";
}
else if (random == 2) {
document.getElementById("toChangeColor").style.backgroundColor = "green";
}
else if (random == 3) {
document.getElementById("toChangeColor").style.backgroundColor = "gray";
}
else if (random == 4) {
document.getElementById("toChangeColor").style.backgroundColor = "yellow";
}
else if (random == 5) {
document.getElementById("toChangeColor").style.backgroundColor = "brown";
}
else if (random == 6) {
document.getElementById("toChangeColor").style.backgroundColor = "blue";
}
else if (random == 7) {
document.getElementById("toChangeColor").style.backgroundColor = "black";
}
switch (random) {
case 1:
document.getElementById("picture").src = "time.jpg";
break;
case 2:
document.getElementById("picture").src = "pic2.jpg";
break;
case 3:
document.getElementById("picture").src = "pic3.jpg";
break;
case 4:
document.getElementById("picture").src = "pic4.jpg";
break;
case 5:
document.getElementById("picture").src = "pic5.jpg";
break;
case 6:
document.getElementById("picture").src = "pic6.jpg";
break;
case 7:
document.getElementById("picture").src = "pic7.jpg";
break;
}
}
當我點擊一個按鈕,我需要從5個選擇中的一個隨機圖片顯示在某個區域。我如何使用開關或循環來做到這一點?
你有什麼嘗試 - 向我們展示一些你已經嘗試過的代碼。 – gavgrif
歡迎來到StackOverflow!爲了讓我們更好地爲您提供幫助,能否請您更新您的問題,以便在[**最小,完整和可驗證的示例**]中顯示**相關代碼**(http://stackoverflow.com/幫助/ MCVE)。如果你能讓我們知道你有什麼[**嘗試到目前爲止**](http://meta.stackoverflow.com/questions/261592)來解決你的問題,這也會很有幫助。有關詳細信息,請參閱有關[**如何提出良好問題**](http://stackoverflow.com/help/how-to-ask)的幫助文章,並參加該網站的[**遊覽**](http://stackoverflow.com/tour)) –
我所做的只是使switch語句: – Beth