0

我对 JavaScript 相当陌生,并且使用 switch 语句做了一个非常基本的选择你自己的冒险游戏。当我尝试使用按钮调用我的函数时,它不成功。我的 JavaScript 就是这个。

function chooseGame(){
var troll = prompt("You are walking through a forest on your way to GameStop. You come   across a troll guarding the only bridge to get there, he says if you pay him he will let you by. Do you PAY, KILL, or CHALLENGE HIM TO A GAME OF CHESS?").toUpperCase();

switch(troll){
case'PAY':
  var money = prompt("Do you have money").toUpperCase();
  var amount = prompt("IS it $50?").toUpperCase();

  if (money && amount === "YES"){
console.log("You pay him and steal the game you wanted, get caught, and go to prison.");   
}else{
console.log("He broke your legs and stole your shoes, not like you'd be needing them.");
}
break;

case'KILL':
  var weapon = prompt("Do you have a weapon?").toUpperCase();
  var friend = prompt("Do you have friends with you?").toUpperCase();

  if (weapon || friend === "YES"){
console.log("You break his legs and take his money buying multiple games.");
}else{
console.log("He brings you back to his personal torture chamber and laughs at your pain for all eternity.");
}
break;

case'CHALLENGE HIM TO A GAME OF CHESS':
  var clever = prompt("Are you clever?").toUpperCase();
  var ability = prompt("Are you good at chess?").toUpperCase();

  if (clever || ability === "YES"){
console.log("You beat him and go on your way to GameStop.");
}else{
console.log("You aren't clever or good at chess, then why did you challenge a troll to a game?");
}
break;
 };
}

使用我的 HTML,它看起来像这样。

<html>
<head>
<Script language = "Javacript">
function chooseGame(){
var troll = prompt("You are walking through a forest on your way to GameStop. You come   across a troll guarding the only bridge to get there, he says if you pay him he will let you by. Do you PAY, KILL, or CHALLENGE HIM TO A GAME OF CHESS?").toUpperCase();

switch(troll){
case'PAY':
  var money = prompt("Do you have money").toUpperCase();
  var amount = prompt("IS it $50?").toUpperCase();

  if (money && amount === "YES"){
console.log("You pay him and steal the game you wanted, get caught, and go to prison.");   
}else{
console.log("He broke your legs and stole your shoes, not like you'd be needing them.");
}
break;

case'KILL':
  var weapon = prompt("Do you have a weapon?").toUpperCase();
  var friend = prompt("Do you have friends with you?").toUpperCase();

  if (weapon || friend === "YES"){
console.log("You break his legs and take his money buying multiple games.");
}else{
console.log("He brings you back to his personal torture chamber and laughs at your pain for all eternity.");
}
break;

case'CHALLENGE HIM TO A GAME OF CHESS':
  var clever = prompt("Are you clever?").toUpperCase();
  var ability = prompt("Are you good at chess?").toUpperCase();

  if (clever || ability === "YES"){
console.log("You beat him and go on your way to GameStop.");
}else{
console.log("You aren't clever or good at chess, then why did you challenge a troll to a game?");
}
break;
 };
}
</Script>
</head>

<body>
<button type="button" onclick = "myFunction();">Game Start</button>
</body>
</html>

当我以 .html 形式运行我的代码时,它会显示按钮,但在我单击它时不会执行任何操作。我能够使按钮发出警报,所以我相信这是我的代码没有调用我的函数的问题,如果我做错了什么,有人可以提醒我。

4

3 回答 3

2

没有名为 myFunction() 的函数。你想要选择游戏()

<button type="button" onclick="chooseGame();">Game Start</button>

(还删除了等号周围的间距。这可能不会破坏它,但不是合法的 HTML)。

于 2013-05-18T16:17:00.023 回答
0

利用<button type="button" onclick = "chooseGame()">

于 2013-05-18T16:17:01.917 回答
0
if (weapon || friend === "YES")

你不能那样写。

if (weapon === "YES" || friend === "YES")
于 2014-02-08T17:51:58.377 回答