【问题标题】:how to write on next line using javascript innerHTML如何使用 javascript innerHTML 在下一行写
【发布时间】:2014-05-04 02:22:43
【问题描述】:

这是我的代码:

document.getElementById("customerInfoLabel").innerHTML = "Trip Type: " + gettriptype + "\n Leaving From: " + getdeparture;

我想要这样的东西:

Trip Type: One Way
Leaving From: Melbourne

它目前给我的是这样的:

Trip Type: One Way Leaving From: Melbourne

【问题讨论】:

  • 你试过用
    标签代替 \n 吗?

标签: javascript jquery html innerhtml


【解决方案1】:

改用<br/>

document.getElementById("customerInfoLabel").innerHTML = "Trip Type: " + gettriptype + "<br/> Leaving From: " + getdeparture;

【讨论】:

    【解决方案2】:

    你可以在网上查到。

    两种选择:

    • 使用&lt;br&gt;标签代替\n
    • 使用样式:

      #customerInfoLabel {
          whitespace: pre;
      }
      

    了解 html 上的空白行为。 developer.mozilla.org 是一个很好的来源。

    【讨论】:

      【解决方案3】:

      正如其他人所说,使用 br 而不是 /n 工作JSFiddle

      document.getElementById("customerInfoLabel").innerHTML = "Trip Type: " + gettriptype + "<br>Leaving From: " + getdeparture;
      

      【讨论】:

        猜你喜欢
        • 2012-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多