我是Javascript的新手,我从一些网站上学到了我所知道的一切。所以我不知道我是否使用旧版本的Javascript,或者是否有其他软件的代码,如jQuery。我所知道的是我的代码不想转到else语句。
<!Doctype html>
<html>
<body>
<center>
<script>
confirm("Are you ready to play!");
var age = prompt("What/'s your age?");
if(age <= 18)
{
document.write("you may play However i take no responsibility for your actions");
}
else
{
document.write("Go ahead but your of age!");
}
document.write("Snow White and Batman were hanging out at the bus stop, waiting to go to the shops. There was a sale on and both needed some new threads. You've never really liked Batman. You walk up to him.");
document.write("Batman glares at you.");
var userAnswer = prompt('Are you feeling lucky, punk?');
if (userAnswer = "yes")
{
document.write("Batman hits you very hard. It's Batman and you're you! Of course Batman wins!");
}
else
{
document.write("You did not say yes to feeling lucky. Good choice! You are a winner in the game of not getting beaten up by Batman.");
}
var feedback = prompt("how good was the game out of 10?");
if (feedback >= 8)
{
document.write ("This is just the beginning of my game empire. Stay tuned for more!");
}
else
{
document.write("I slaved away at this game and you gave me that score?! The nerve! Just you wait!");
}
</script>
</center>
</body>
</html>
答案 0 :(得分:8)
=
是作业,如果您指定的是真值,则为true。您需要(严格)equality test:===
(或==
)
答案 1 :(得分:0)
使用==而不是=。
无论如何,您应该查看Java Language Specification, §5.6.2,和其他有关=和==和===的网站。
其次,document.write删除整个文档并用文本替换它。如果您不想删除整个文档,请进行一个名为:
的部门<html>
<head>
</head>
<body>
<div id='gameText'>
Are you ready to play?
</div>
</body>
</html>
基本上,由于您不熟悉javascript,我会解释它的作用。
标签代表分区,它基本上只是一个部分。
id部分用gameText命名该部分。
它是简单的HTML,只是在伪代码形式中意味着:
在HTML文档中创建名为gameText的新部分
对于javascript,您应该使用innerHTML标记来影响该部分,而不是进行文档编写。
使用javascript获取该部分的名称或ID,并更改它的innerHTML。
代码应如下所示
<html>
<head>
<script>
confirm("Are you ready to play!");
var age = prompt("What/'s your age?");
if(age <= 18){
document.getElementById('gameText').innerHTML = "you may play However i take no responsibility for your actions";
}
else
{
document.getElementById('gameText').innerHTML = "Go ahead but you're of age!";
}
</script>
</head>
<body>
<div id='gameText'>
Are you ready to play?
</div>
</body>
</html>
此外,请修正您的拼写错误。我可以清楚地看到你正在尝试创建一个你将要在某个地方发布的HTML / JS游戏,或者你只是为了好玩。
无论哪种方式,如果它有语法和拼写错误,没有人会玩你的游戏。
所有这些,上面和下面的一点点只是帮助你改善这一点的方法吗?
好吧无论如何,当你加载HTML文档时,你没有自动运行你的游戏,你真的应该有一个启动游戏的按钮,所以当你进入页面时没有人对弹出窗口感到恼火。