为什么我的JavaScript代码没有调用我的函数?

时间:2013-05-18 16:12:32

标签: javascript html function

我是一个相当陌生的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,它显示按钮但是当我点击它时什么都不做。我能够使按钮警报,所以我认为这是我的代码没有调用我的功能的问题,如果我做错了什么,有人可以提醒我。

3 个答案:

答案 0 :(得分:2)

没有名为myFunction()的函数。你想要chooseGame()

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

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

if (weapon || friend === "YES")
你不能这样写。

if (weapon === "YES" || friend === "YES")