gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:15:01 25 4
gpt4 key购买 nike

这是一个 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>

最佳答案

提示1

查看浏览器的错误控制台以获得更多提示。

Console example.

语法错误意味着无法解释代码,因为解析器发现了一些它没有预料到的东西并且无法从中恢复。您是否能看到口译员可能期望但实际上没有的任何内容?

提示2

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

提示3

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

您是否正确平衡了牙套?

关于javascript - 在浏览器中单击按钮时,代码不会执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7844154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com