18910140161

JavaScript-Button调用的函数与它应该调用的堆栈溢出函数完全不同

顺晟科技

2022-10-19 14:21:35

70

我想发生的是,当我在键入“是”后点击按钮时,它会说“老头:很高兴听到你的声音,你叫什么名字?”但相反,它调用了一个不同的函数,对尚未添加的问题表示else答案。这可能是我太笨了,因为我是新手,对HTML/JSS很不擅长,但如果有人知道为什么会发生这种情况,并让我知道它会大大增加

html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>repl.it</title>
    <link href="style.css" rel="stylesheet"type="text/css"/>
    <script src="script.js"></script>
  </head>
  <body onload="onload()">  
    <center>
    <h1 class="Title"> Text Adventure HMTL </h1>
    <h5> Please write your answer directly how it was written after the question</h5>
    <br><br>
    </center>
    <p id="currentQuestion">
    <p id="givenAnswers"></p>

    <input type="textbox" id="answer" placeholder="Type your answer here">
     <input  id="enter" type="button" onclick="wakeUp()" value="enter">
    
  </body>
</html>

js:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>repl.it</title>
    <link href="style.css" rel="stylesheet"type="text/css"/>
    <script src="script.js"></script>
  </head>
  <body onload="onload()">  
    <center>
    <h1 class="Title"> Text Adventure HMTL </h1>
    <h5> Please write your answer directly how it was written after the question</h5>
    <br><br>
    </center>
    <p id="currentQuestion">
    <p id="givenAnswers"></p>

    <input type="textbox" id="answer" placeholder="Type your answer here">
     <input  id="enter" type="button" onclick="wakeUp()" value="enter">
    
  </body>
</html>

您可以在这里测试它并查看它的功能:https://idkwthisgoingon.kitten3604.repl.co/


顺晟科技:

编辑的答案

您正在使用如果不正确:不像您认为的那样工作。 Do:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>repl.it</title>
    <link href="style.css" rel="stylesheet"type="text/css"/>
    <script src="script.js"></script>
  </head>
  <body onload="onload()">  
    <center>
    <h1 class="Title"> Text Adventure HMTL </h1>
    <h5> Please write your answer directly how it was written after the question</h5>
    <br><br>
    </center>
    <p id="currentQuestion">
    <p id="givenAnswers"></p>

    <input type="textbox" id="answer" placeholder="Type your answer here">
     <input  id="enter" type="button" onclick="wakeUp()" value="enter">
    
  </body>
</html>

在您的支票中。

总是返回true,因为非空字符串在转换为布尔值时总是被认为是true,因此if语句中的第二个条件是true

编辑

虽然我已经回答了这个问题,但我想给您一个建议:

尽可能地泛化代码。它将使您的代码更短、更易读,并且在为代码添加功能和复杂性时也会有所帮助。

下面是我尝试遵循以下建议的对话框机制的实现:

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航