【发布时间】:2015-10-16 14:09:15
【问题描述】:
我创建了一个名为“compare”的函数,并希望从一个 ID 为“start”的按钮运行它
<input type="button" id="start" onClick="compare(userChoice, computerChoice)" value="Start">
但是当它被按下时,功能没有加载,我也试图在这个中显示它
document.getElementById("score").innerHTML = compare(userChoice, computerChoice);
但它的问题是它在页面加载时开始
<script>
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";
}
var compare = function(choice1, choice2){
if(choice1 === choice2){
return "The result is a tie!"
}
else if(choice1 === "rock"){
if(choice2 === "scissors"){
return "rock wins";
}
else{
return "paper wins"
}
}
else if(choice1 === "paper"){
if(choice2 === "rock"){
return "paper wins";
}
else{
return "scissors wins"
}
}
else if(choice1 === "scissors"){
if(choice2 === "paper"){
return "scissors wins";
}
else{
return "rock wins"
}
}
};
document.getElementById("player").innerHTML = userChoice;
document.getElementById("computer").innerHTML = computerChoice;
document.getElementById("score").innerHTML = compare(userChoice, computerChoice) ;
</script>
【问题讨论】:
-
这已经被问过了。 stackoverflow.com/search?q=rock
标签: javascript html function innerhtml