【问题标题】:Replacing a string of code in html in JS [closed]在JS中替换html中的一串代码[关闭]
【发布时间】:2016-07-08 01:39:03
【问题描述】:

我是新来的,这是我提出的第一个问题。这对你们中的一些人来说可能很容易,甚至都不好笑。我需要替换和/或删除一个字符串以使其适合。我试过在结尾处剪掉多条线,但我似乎无法让它为我工作。另外我正在使用记事本++

原字符串:

var num1;
var num2;
var answer;
num1 = prompt("Enter a number ");
num2 = prompt("Enter a number ");
document.write("You entered " + num1 + " and " + num2);
document.write("<br>");
answer = prompt("Enter the sum of " + num1 + " and " + num2);
document.write("You entered " + answer);
document.write("<br>");
num1 = parseInt(num1);
num2 = parseInt(num2);
var sum = num1 + num2;
document.write("The sum is " + sum);
<html>
  <body>
  </body>
</html>

我需要插入的字符串:

if (answer == sum) {
  document.write("That is correct!");
} else {
  document.write("That is not correct. The correct answer is " + sum);
}

【问题讨论】:

  • string that you need to insert 复制到结束script 标记上方时会发生什么?
  • 让我试试,我就放在上面,document.write("The sume is " + sum);
  • 它的行为与没有我需要添加的字符串时相同。它并没有说明我添加的是对还是错。
  • 您可以查看JSFiddle
  • 你的代码和@Rajesh 的小提琴的区别似乎是var sum = num1 + num2 后面没有分号。另一方面,在 fiddle 的底部有这样一条消息:document.write is disallowed in JSFiddle environment and might break your fiddle. 那么 fiddle 做了什么?

标签: javascript html notepad++


【解决方案1】:

创建一个&lt;span&gt; 元素来存放完成消息:

<span id="answerMessage"></span>

然后您可以通过以下方式应用该消息:

document.getElementById("answerMessage").innerHTML = "Correct!";

这样,这条消息就不需要擦除或打印了。

编辑

如果您的应用程序设计为看起来像具有完整打印历史的基于命令的系统,则忽略此解决方案。我提供了这个,因为这在你的问题中并不明显。

【讨论】:

  • 我认为我的问题是要求完整打印的历史记录,但现在它要求我添加列出的字符串并使其正常工作。从字面上看,对于第二个问题,我必须添加它,它要求我对其进行多次测试。
【解决方案2】:
var num1;
var num2;
var answer;
num1 = prompt("Enter a number ");
num2 = prompt("Enter a number ");
document.write("You entered " + num1 + " and " + num2);
document.write("<br>");
answer = prompt("Enter the sum of " + num1 + " and " + num2);
document.write("You entered " + answer);
document.write("<br>");
num1 = parseInt(num1);
num2 = parseInt(num2);
var sum = num1 + num2;
if (answer == sum) {
  document.write("That is correct!");
} else {
  document.write("That is not correct. The correct answer is " + sum);
}
document.write("The sum is " + sum);

这样的东西应该可以工作

【讨论】:

  • 如果我可能会问,你用什么来运行这个字符串?因为如果它在记事本++以外的其他东西上对你有用,那不适合我=/
  • 我用的是JSFiddle,复制JS部分的sn-p代码,点击左上角的运行图标运行
  • 我不知道你能不能打开这个,但这是我得到的。
  • 但我在 JSFiddle 中测试了我的代码,它可以工作
【解决方案3】:

这在此处有效(在 Firefox 中):

<!DOCTYPE html>
<html>
  <body>
    <script>
      var num1;
      var num2;
      var answer;
      num1 = prompt("Enter a number ");
      num2 = prompt("Enter a number ");
      document.write("You entered " + num1 + " and " + num2);
      document.write("<br>");
      answer = prompt("Enter the sum of " + num1 + " and " + num2);
      document.write("You entered " + answer);
      document.write("<br>");
      num1 = parseInt(num1);
      num2 = parseInt(num2);
      var sum = num1 + num2;
      document.write("The sum is " + sum);
      document.write("<br>");
      if (answer == sum) {
        document.write("That is correct!");
      } else {
        document.write("That is not correct. The correct answer is " + sum);
      }
    </script>
  </body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    相关资源
    最近更新 更多