javascript下一行(\ n)字符串无效

时间:2015-05-06 10:04:34

标签: javascript

嗨,这是没有意义的我试图放弃\ n来打破我的句子,但它似乎不起作用,我的代码在下面

 "Address:"+location.addressLine1 + " " + location.addressLine2 +"\n "+"Store Name:\n"+ location.storeName +" "+"Geofence:\n"+ location.maxGeofence+" "+"City:\n"+location.city

我的目标是将所有行都放在新行中,如下所示

Address:xxxxxxx

Store Name:xxxxxx

Gofence:xxxxxx

City:xxxxxxx

但我继续

 Address:xxxxxxx Store Name:xxxxxx Gofence:xxxxxx  City:xxxxxxx

4 个答案:

答案 0 :(得分:0)

不使用\n,而是使用<br/>

<br />HTML element for a line break<br />允许在浏览器中呈现HTML时显示为新行

答案 1 :(得分:0)

在html \ n中无效。将其替换为<br/>或将代码放在<pre>标记内。

  1. br - 将在HTML中创建一个新行
  2. pre - 里面的代码将在文本文件中形成(它会理解\ n),但你必须设置样式。

答案 2 :(得分:0)

在HTML中使用<br/>,或者,如果您希望继续使用\n,请使用 php.js 中的nl2br函数:

function nl2br (str, is_xhtml) {
    var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
    return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
}

这会将每\n转换为<br/>

答案 3 :(得分:0)

以下是换行符的解决方案

"Address:"+location.addressLine1 + " " + location.addressLine2 +"<br>"+"Store Name:<br>"+ location.storeName +" "+"Geofence:<br>"+ location.maxGeofence+" "+"City:<br>"+location.city

注意:\ n仅适用于PHP而不是Html,所以\ n在这里不起作用。