将js分成多行

时间:2017-06-30 18:12:26

标签: javascript

在我工作的网页上,我有一个标有网站的谷歌地图,以及包含该网站联系信息的标签。在像波士顿或纽约这样的密集区域,我在一个标签内有多个网站的联系信息。我遇到的问题是,当我将所有网站的联系信息放入如下所示的单个文本行时,我才能正常运行。

var labelBoston = [
  new GInfoWindowTab("Center", "<b>Beth Israel Deaconess Medical Center</b><br>Jon Doe, MD<br>Harvard Medical School<br>address line here<br>Boston, MA 02215<br>Phone: xxx-xxx-xxxx<br>Fax: xxx-xxx-xxxx<br><a href='mailto:jondoe@BIDMC.com'>jondoe@BIDMC.com</a><br><br><b>Boston Medical Center</b><br>Jane Doe, Study Coordinator<br>Dept. of Obstetrics-Gynecology<br>address line here<br>100th Floor<br>Boston, MA 02118<br>Phone: xxx-xxx-xxxx<br><a href='mailto:janedoe@BMC.com'>janedoe@BMC.com</a>"),
];

虽然这有效,但维护起来却是一件巨大的痛苦。特别是在其他有> 5个地点需要处理的城市。我想把它分成如下:

var labelBoston = [
  new GInfoWindowTab("Center", "
<b>Beth Israel Deaconess Medical Center</b><br>Jon Doe, MD<br>Harvard Medical School<br>address line here
<br>Boston, MA 02215<br>Phone: xxx-xxx-xxxx<br>Fax: xxx-xxx-xxxx
<br><a href='mailto:jondoe@BIDMC.com'>jondoe@BIDMC.com</a>

<br><br>

<b>Boston Medical Center</b><br>Jane Doe, Study Coordinator
<br>Dept. of Obstetrics-Gynecology<br>address line here
<br>100th Floor<br>Boston, MA 02118<br>Phone: xxx-xxx-xxxx
<br><a href='mailto:janedoe@BMC.com'>janedoe@BMC.com</a>"),
    ];

或类似的东西,以便保持这些信息更容易管理。但是我对单数文本行的任何变化都会完全破坏一切。有人可以帮我找到解决方案吗?

2 个答案:

答案 0 :(得分:0)

我的建议是逃避回车。也就是说,只需在按下Enter键之前放置\。但是我可以想到4种不同的方式。

let a = `There's a few ways about multi-line strings in Javascript.
In ES6, template literals allow multiple lines,`

let b = "but you can also have multiple lines by \
escaping the carriage return,"

let c = "or by using " +
"String concatenation,"

let d = [
    "or you can join",
    "items in an array."
].join(" ")

console.log(a,b,c,d);

答案 1 :(得分:0)

将每一行或几行分成它自己的字符串。例如,

"Beth Israel Deaconess Medical Center
Jon Doe, MD
Harvard Medical School
address line here 
Boston, MA 02215
Phone: xxx-xxx-xxxx
Fax: xxx-xxx-xxxx 
jondoe@BIDMC.com"

可能是,

var string = '';
string += 'Beth Israel Deaconess Medical Center<br />';
string += 'Jon Doe, MD.<br />';
string += 'Harvard Medical School<br />';
string += 'address line here <br />';
string += 'Boston, MA 02215<br />';
string += 'Phone: xxx-xxx-xxxx<br />';
string += 'Fax: xxx-xxx-xxxx <br />';
string += 'jondoe@BIDMC.com<br />';

然后在最后连接你的字符串(var total_string = string + string1 + string2 +...b),然后说,

var labelBoston = [ 
  new GInfoWindowTab("Center", total_string);
];