-3

我是 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>
4

2 回答 2

8

=是一个分配,如果您分配一个真值,它将是真的。你想要一个(严格的)平等测试:(=====

于 2013-06-27T18:36:00.243 回答
0

使用 == 而不是 =。

无论如何,您应该查看https://www.quora.com/What-is-the-difference-between-and-operator-in-javascript和其他有关 = 和 == 和 === 的网站。

其次,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 文档时自动运行,你应该有一个启动游戏的按钮,这样当你进入页面时,没有人会因为弹出窗口而烦恼。

于 2018-05-22T22:38:47.913 回答