在浏览器中单击按钮时,代码将不会执行

时间:2011-10-21 01:42:13

标签: javascript

这是一个javascript作业练习。问题是第20行的语法错误与我看起来正确,以及在浏览器中单击按钮1时未执行的代码。代码实际上已部分执行,直到我开始尝试匹配我的花括号。不显示不同的场景,但显示的是场景编号。这是一个if和else if语句,其中单击按钮以在单击按钮时将用户带到下一个场景。我昨天和今天都待了大约20个小时。有什么建议?这是代码:

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1       /DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>The mysterious road</title>
  <script type="text/javascript">

  var curScene = 0;

  var option = 0;

  function changeScene(option){
  var message = "";


  if (curScene === 0) {
curScene=1;
message = "Your journey begins at fork in the road.";

     else if(curScene ==1){ //This line is the syntax error
    if(option==1){
    curScene = 2;
    message = "You have arrived at a cute little house in the woods.";
    }   
    else{
     curScene = 3;
    message = "You are standing on the bridge overlooking a peaceful stream";
    }
   end curScene 1
    else if (curScene ==2){
    if(option==1){
    curScene = 4;
    message = "Peeking through the window, you see a witch inside the house.";

else{
     curScene = 5;
}       message = "Sorry, a witch lives in the house and you just become part of  her     stew.";
}

 }//ends curScene ==2
    else if (curScene ==3){
    if(option==1){
    curScene =6;
    message = "Sorry, a troll lives on the other side of the bridge and you just     became     his lunch";

else{
    curScene = 7;
    message = "Your stare is interupted by the arrival of a huge troll.";
}
    }

 }//ends curScene 3
    else if (curScene ==4){
    if(option==1){
    curScene = 8;


else{
    curScene = 5;
    message = "Sorry, you became part of the witche's stew.";
}
    }
}//ends curScene==4
    else if (curScene ==5){
    curScene = 0;
    }
    else if (curScene == 7){
        if (option ==1){
            curScene = 6;
            message = "Sorry, you've become the troll's tasty lunch.";
        }
        else{
            curScene = 9;
        }
    }
    else if (curScene == 8){
        message = "To be continued!";
    }
    else if (curScene == 9){
        message = "To be continued!";
    }

    }
  }//ends curScene 5
  else if (curScene == 7){
curScene = 6;

if(option==1){
    message = "Sorry, you became the troll's tasty lunch.";
}
 }//end curScene 7
 else {
curScene = 9;
 }//end of curScene 7

 }//end if 


 document.getElementById("sceneimg"). src = "scene0" + curScene + ".png;" 
alert(message);
 }//end of function

 </script>
 </head>
 <body>
 <div style="margin-top:100px; text-align:center">
 <p><img id="sceneimg" src="sfa/scene0.png" alt="Stick Figure" /></p>

 Enter here for a glorious adventure!
 <input type="button" id="option" value="1" onclick="changeScene(1)" />
 Enter this gate for the surpirse of your life!
 <input type="button" id="option" value="2" onclick="changeScene(2)" />
 </div>

</body>
</html>

2 个答案:

答案 0 :(得分:4)

提示1

查看浏览器的错误控制台以获取其他提示。

Console example.

语法错误意味着无法解释代码,因为解析器发现了一些它没有预料到并且无法恢复的内容。你能看到口译员所期待的但不存在的东西吗?

提示2

如果您无法弄清楚,请再次仔细检查您的控制字符。确保在打开的地方关闭物品。它们必须是对称的。

提示3

将鼠标悬停在下面的区块上以获得另一个提示。

  

你是否正确平衡了牙箍?

答案 1 :(得分:-1)

message = "Peeking through the window, you see a witch inside the house.";

else {
    curScene = 5;
}
之前

缺少}