gpt4 book ai didi

javascript - 在 JavaScript 中使用基于用户输入的条件语句的问题

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

我正在为一些 friend 建立一个笑话网站,网站上的一个页面我想有一些互动游戏。我习惯了 C++,所以过渡到 javascript 是一个有趣的过程。现在页面上只有一个段落元素、一个文本输入框和一个按钮。单击按钮时,它应该调用 playGame() 函数。 playGame 函数查看段落元素(“游戏”)中的内容,然后查看用户输入并生成适当的结果。我只是使用一组条件来完成这个,但由于某种原因,该功能似乎没有改变段落元素,从而使整个事情变得毫无意义,经过并进行了一些修改,但我无法修复问题。这是代码:

<html>

<script>

function playGame()
{
test=document.getElementById("game");
var userInput=gameInput=document.getElementById("gameInput").value;

if (test.innerHTML=="Question_1")
{
if (userInput.toLowerCase()=="yes")
{
test.innerHTML="Ending_Result_1";
}
else if (userInput.toLowerCase()=="no")
{
test.innerHTML="Question_2";
}
else
{
test.innerHTML="Ending_Result_2";
}
}
else if (test.innerHTML=="Question_2")
{
if (userInput.toLowerCase()=="yes")
{
test.innerHTML="Positive_Ending";
}
else if (userInput.toLowerCase()=="no")
{
test.innerHTML="Ending_Result_3";
}
else
{
test.innerHTML="Ending_Result_4";
}
else
{
test.innerHTML="Refresh_page_message";
}
}
</script>


<head>

<title>CptTuna's Whip or No Whip</title>

</head>

<body>

<p id="game">"Question_1"</p>

<input id="gameInput" type="text">
<button type="button" onclick="playGame()">Answer</button>

</html>

为了适当起见,我只是将实际的文本内容更改为通用短语。将不胜感激任何有关为什么该功能无法正常执行的帮助。

最佳答案

选择一个:

  • 在条件语句中包含引号 (""),或者……
  • 从 HTML 中删除引号

'Question_1' != '"Question_1"'
请注意,您可以使用单引号和双引号的组合来避免转义

关于javascript - 在 JavaScript 中使用基于用户输入的条件语句的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15054838/

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