javascript - 使用 jQuery/JavaScript 做出答案后删除文本

标签 javascript jquery html dom

我认为我的逻辑遇到了问题。应该发生的是,用户将输入所提出问题的答案。提交问题后,当前文本(第一个问题)就会消失,数组中的下一个问题会出现在第一个问题的位置。

目前,问题仍在不断堆积,而没有删除前一个问题。每次我尝试添加更多代码来删除该元素时,都会出现错误。

请帮忙!我正在慢慢添加此代码功能,但现在我陷入了添加/删除文本的困境。

$(function() {
  $("#submit").on("click", askQuestion)

  var questions = [{
      question: "Is the price higher or lower than $40.00?",
      answer: "higher"
    },
    {
      question: "Is the price higher or lower than $100.00?",
      answer: "higher"
    },
    {
      question: "Is the price higher or lower than $50.00?",
      answer: "lower"
    }
  ];

  function askQuestion() {
    if (answer && document.querySelector("#user-answer").value == answer) {
      document.querySelector("#correct").style.display = "block";
      document.querySelector("#sorry").style.display = "none";
      answer = randomQuestion()
    } else {
      document.querySelector("#correct").style.display = "none";
      document.querySelector("#sorry").style.display = "block";

    }
  }

  function randomQuestion() {
    var question = questions[Math.floor(Math.random() * questions.length)];

    document.querySelector("#user-answer").value = "";

    var element = document.createElement("div");
    element.innerHTML = question.question;

    $("#question").append(element.firstChild);

    return question.answer;
  }

  var answer = randomQuestion();
});
<html>
<head>
  <meta charset="UTF-8">
  <title>Game time</title>
</head>
<body>
  <h1>Question and Answer</h1>
  <div>
    <h2 id="question"></h2>
  </div>
  <label for="text">Answer:</label>
  <input id="user-answer" type="text" value="">
  <button id="submit" type="submit">Submit</button>
  <p id="sorry" style="display: none">Sorry...</p>
  <p id="correct" style="display: none">You got it!</p>
  <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
  <script src="main.js"></script>
</body>
</html>

最佳答案

当您应该更改问题时,您只需使用append,而不是附加到它。

您可以使用: $("#question").html(element.firstChild);

而不是$("#question").append(element.firstChild);

$(function() {

  $("#submit").on("click", askQuestion)

  var questions = [{
      question: "Is the price higher or lower than $40.00?",
      answer: "higher"
    },
    {
      question: "Is the price higher or lower than $100.00?",
      answer: "higher"
    },
    {
      question: "Is the price higher or lower than $50.00?",
      answer: "lower"
    }
  ];

  function askQuestion() {
    if (answer && document.querySelector("#user-answer").value == answer) {
      document.querySelector("#correct").style.display = "block";
      document.querySelector("#sorry").style.display = "none";
      answer = randomQuestion()
    } else {
      document.querySelector("#correct").style.display = "none";
      document.querySelector("#sorry").style.display = "block";

    }
  }


  function randomQuestion() {
    var question = questions[Math.floor(Math.random() * questions.length)];

    document.querySelector("#user-answer").value = "";

    var element = document.createElement("div");
    element.innerHTML = question.question;

    $("#question").html(element.firstChild);

    return question.answer;

  }

  var answer = randomQuestion();

});
<html>

<head>
  <meta charset="UTF-8">
  <title>Game time</title>
</head>

<body>
  <h1>Question and Answer</h1>
  <div>
    <h2 id="question"></h2>
  </div>
  <label for="text">Answer:</label>
  <input id="user-answer" type="text" value="">
  <button id="submit" type="submit">Submit</button>

  <p id="sorry" style="display: none">Sorry...</p>
  <p id="correct" style="display: none">You got it!</p>

  <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
  <script src="main.js"></script>
</body>

</html>

关于javascript - 使用 jQuery/JavaScript 做出答案后删除文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51142832/

相关文章:

html - 相对于外部 div 的宽度和高度,使输入框的宽度和高度为 100%

html - 动态生成的 div 之间的 css 填充

javascript - 使用 Javascript 循环浏览文本

javascript - 使用filter()函数删除嵌套数组中的元素

javascript - 获取包含日期的文本框的值?

jquery - 获取不带 'px' 的元素的行高

Jquery 格式化 - 在另一个函数完成后运行函数

javascript - 使一个函数影响其他函数

javascript - Angular 2 ChangeDetectionStrategy.OnPush 在输入不改变时触发 ngAfterViewChecked

html - 试图在 Bootstrap 4 中扩展行宽