I'我是javascript新手;我不知道为什么它不会跳到另一个状态

I'm new at javascript and i don't know why it wont jump to the else statment

本文关键字:另一个 状态 我不知道 我是 javascript 新手 为什么      更新时间:2023-09-26

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

=是一个赋值,如果您正在赋值true,它将为true。您想要一个(严格的)相等测试:===(或==

使用==而不是=。

不管怎样,你应该去看看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,并更改其内部HTML。

代码应该看起来像这个

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

相关文章: