继续告诉我,我无法设置“src属性为null”。请帮我解决我目前的这个问题。我不知道,因为我创造了那些ID,但它告诉我它们不存在。我不明白这一点。
var pot = 100;
document.getElementById("answer").innerHTML = "New Game <br> Pot: $" + pot;
document.getElementById("reset").disabled = true;
function pull() {
var slot1 = 0;
var slot2 = 0;
var slot3 = 0;
var slot4 = 0;
/*generate 3 random numbers, assign to corresponding img1,
img2,img3, swap images in table */
for (var i = 1; i <= 4; i++) {
var fruit = Math.floor((Math.random() * 4) + 1);
document.getElementById(i).src = "img" + fruit + ".gif";
//assign each of the 3 random numbers to a slot value
if (i == 1) {
slot1 = fruit;
} else if (i == 2) {
slot2 = fruit;
} else if (i == 3) {
slot3 = fruit;
} else {
slot4 = fruit;
}
}
//add 25 to the pot if the result is 3 of a kind
if (slot1 == slot2 && slot2 == slot3 || slot1 == slot2 && slot2 == slot4 || slot2 == slot3 && slot3 == slot4) {
pot = pot + 25;
document.getElementById("answer").innerHTML =
"Jackpot, 3 of a kind! You win $25.00, <br> Pot: $" + pot;
}
//add 50 to pot if they get four of a kind
else if (slot1 == slot2 && slot3 == slot4 && slot2 == slot3 && slot4 == slot1) {
pot = pot + 50;
}
//add 10 to pot if the result is a pair
else if (slot1 == slot2 || slot2 == slot3 || slot1 == slot3) {
pot = pot + 10;
document.getElementById("answer").innerHTML = "A pair! You win $10.00. <br> pot: $" + pot;
} else {
pot = pot - 120;
if (pot <= 0) {
document.getElementById("answer").innerHTML = "pot is empty, you are out of cash.";
document.getElementById("stop").disabled = true;
document.getElementById("reset").disabled = false;
} else {
document.getElementById("answer").innerHTML = "No match! You loose $20.00. <br> Pot: $" + pot;
}
}
}
function start() {
pot = 100;
document.getElementById("reset").disabled = true;
document.getElementById("stop").disabled = false;
document.getElementById("answer").innerHTML = "New Game <br> Pot: $" + pot;
document.getElementById(1).src = "img0.gif";
document.getElementById(2).src = "img0.gif";
document.getElementById(3).src = "img0.gif";
document.getElementById(4).src = "img0.gif";
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link href="reset.css" rel="stylesheet" type="text/css">
<link href="slotMachine.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="container">
<div id="title">
<h1>Slot Machine!</h1>
<h1>Try Your Luck!!</h1>
</div>
<div id="game">
<table>
<tr>
<td><img src="img0.gif" id="1"></td>
<td><img src="img0.gif" id="2"></td>
<td><img src="img0.gif" id="3"></td>
<td><img src="img0.gif" id="4"></td>
</tr>
</table>
</div>
<div id="play">
<button id="stop" onClick="pull()"> Pull </button>
<button id="reset" onClick="start()"> Reset </button>
</div>
<div id="result">
<p id="answer"></p>
</div>
</div>
</body>
<script src="slotMachine.js">
</script>
</html>