我有一个外部 javascript 文件,它有两个函数,每次页面加载时都应该调用它们。但是,两者都没有。如果我注释掉js文件中调用tester()函数的那一行,那么runGame()就会被调用,但是如果tester()没有被注释,那么都不运行。但是,如果我将 tester() 函数调用放在 runGame() 调用之后,那么 runGame() 可以工作,但 tester() 仍然无法工作。
tester() 甚至不会自行运行,因此我在该函数中可能做错了什么。谁能告诉我我做错了什么?
html
<html>
<head>
<script type="text/javascript" src="rock_paper_scissors.js">
</script>
</head>
<body>
<p id="game">Test</p>
<br />
<input type="button" value="Play again" onClick="tester()"></input>
</body>
rock_paper_scissors.js
var info = document.getElementById("game");
var tester = function(){
document.getElementById('game').innerHTML = 'hello';
//info.innerHTML("HI");
};
var compare = function(choice1, choice2){
if(choice1 == choice2)
document.write("The result is a tie!");
else if(choice1 == "rock"){
if(choice2 == "scissors")
document.write("You win! Rock wins");
else
document.write("The computer wins! Paper wins");
}
else if(choice1 == "paper"){
if(choice2 == "rock")
document.write("You win! Paper wins");
else
document.write("The computer wins! Scissors wins");
}
else if(choice1 == "scissors"){
if(choice2 == "rock")
document.write("The computer wins! Rock wins");
else
document.write("You win! Scissors wins");
}
else
document.write("You didn't pick a real choice");
};
var runGame = function(){
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
document.write("<p>You picked " + userChoice + "</p>");
document.write("<p>The Computer picked " + computerChoice + "</p>");
compare(userChoice, computerChoice);
};
runGame();
tester();