【问题标题】:How to brake line in javascript ( "\n" is not working )如何在 javascript 中刹车线(“\n”不起作用)
【发布时间】:2020-03-16 23:48:45
【问题描述】:

我正在尝试在 JavaScript 中换行,但它不起作用,我试过了:

document.write(a)\n;
document.write(b);

document.write(a);
"<br>"+document.write(b);

document.write(e);<br>
document.write(e);

但是没有人在锻炼:(

有人可以帮帮我吗?

【问题讨论】:

  • document.write(a + '&lt;br&gt;');document.write('&lt;br&gt;' + b);
  • document.write(a+ '&lt;br&gt;' + b),假设 ab 已被声明或创建。
  • document.write(a + "\n")
  • 我有时间,我很无聊,所以如果你愿意,花点时间写更多的代码和关于你的目标的解释,我会看看它。这似乎是你第一次在这里发帖,你会被 cmets 摧毁,但别担心,我们都在第一次发帖时被摧毁了。

标签: javascript


【解决方案1】:
<script type="text/javascript">
    document.write(a);
    document.write("<br>");
    document.write(b);

    document.write(a + "<br>" + b);
</script>

How do I create a new line in Javascript?

【讨论】:

    【解决方案2】:
    document.write(a+"\n");or document.write(a+"<br>");
    

    【讨论】:

      【解决方案3】:

      使用Document.Write 通常不是一件好事,因为它可能不会像你认为的那样做,但无论如何。

      在您的代码中,假设 abe 是字符串变量,您需要将换行符放在括号内。

      document.write(a+"\n");
      document.write(b+"<br/>");
      

      更新

      document.write 没有任何问题,但如果您不了解它的作用,可能会感到困惑。

      您传递给函数的任何字符串都附加到文档的末尾

      例子:

      <html>
      <body>
      <h1>
      <script> document.write("My Header!"); </script>
      </h1>
      </body>
      </html>
      

      您可能认为这会写出文本“我的标题!”在 h1 标签内,但结果实际上是这样的

      <html>
      <body>
      <h1>
      <script> document.write("My Header!"); </script>
      </h1>
      </body>
      </html>
      My Header!
      

      如果您有&lt;iframe&gt; 或类似名称,并且您想从头开始生成整个文档,那么Doc​​ument.write 非常棒。使用它来更改当前页面不起作用(非常好)。要更改当前页面,您需要查看 DOM Manipulation。

      【讨论】:

      • 我可以用什么来代替“document.write”?
      • 取决于你的最终目标
      • 我真的不知道,我只是从javascript xD开始
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-01
      • 1970-01-01
      • 2020-10-09
      • 2017-02-03
      • 1970-01-01
      相关资源
      最近更新 更多