【问题标题】:why doesn't javascript print new row with \n?为什么 javascript 不使用 \n 打印新行?
【发布时间】:2018-11-08 12:52:58
【问题描述】:

我的 JS 有问题,每当我在脚本标签中写“\n”时,它不会打印新行。我是 Javascript 新手,所以有人可以向我解释问题是什么吗?

这是我练习的代码:

<html>
<head>
  <meta charset="utf-8">
  <script>
    var output;
    var number = window.prompt("Enter mobile no:  \n");
    if( number>10000000 && number<99999999){
     var ps = parseInt(number/1000000);
     var vs = parseInt((number/1000)%1000);
     var ts = number%1000;
     output = "0" + ps + "/" + vs + "-" + ts +" ";
      document.write("Mobile no. is : " + output + " "  + "\n");


   if( ps == 70 || ps == 71 || ps == 72 ){
      document.write("Mobile no. is T-Mobile " + "\n");
   }
   if( ps == 75 || ps == 76){
      document.write("Mobile no. is One " + "\n");
   }
   if( ps == 77 || ps == 78){
      document.write("Mobile no. is Vip " + "\n");
   }

    }
  </script>

</head>

<body>

//not important

</body>
</html>

【问题讨论】:

标签: javascript newrow


【解决方案1】:

您正在输出到浏览器,因此 HTML 是相关的,而不是纯文本。只需将“\n”替换为“&lt;br&gt;”即可。

【讨论】:

    【解决方案2】:

    您正在向页面写入 HTML。它不识别新行。您需要使用“&lt;br&gt;”标签。

    【讨论】:

      【解决方案3】:

      如果您正在编写 .html 文件,请使用 &lt;br&gt; 标记,如下所示:

      document.write("<br>");
      

      否则,您可以使用"\n"

      【讨论】:

        猜你喜欢
        • 2014-04-24
        • 2021-12-09
        • 1970-01-01
        • 2022-12-20
        • 1970-01-01
        • 2020-09-26
        • 2021-11-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多