2017-06-30 5 views
0

私は仕事をしているウェブページに、そのサイトの連絡先情報を含むラベルとともに、サイトにマークが付けられたGoogleマップを持っています。ボストンやニューヨークのような密集地域では、単一のラベルに複数のサイトの連絡先情報があります。私が抱えている問題は、すべてのサイトの連絡先情報を以下のような単一のテキスト行に入れると、これを正しく機能させることしかできないということです。jsを複数の行に分割する

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:[email protected]'>[email protected]</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:[email protected]'>[email protected]</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:[email protected]'>[email protected]</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:[email protected]'>[email protected]</a>"), 
    ]; 

この情報を管理することは、より管理しやすいものです。しかし、私が単数のテキスト行に行ったバリエーションは、すべてを完全に破壊します。誰かが私に解決策を見つけるのを助けることができるか?

+0

、だろう、最後に1つの大きな文字列に連結するだけです。たとえば、 'Harvard Medical School'は' var school = 'Harvard Medical School
; 'とすることができます。この方法では、文字列の異なるビットを単独で維持するのは簡単です。 –

+0

HTMLをHTMLファイルに格納し、JavaScriptでハードコードするのではなく、ajaxでロードすることをお勧めします。 –

答えて

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

テンプレートリテラルを使用すると、テキスト自体にキャリッジリターンが含まれます。他はしません。 OPなどに向かうだけです。 –

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  
[email protected]" 

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 += '[email protected]<br />'; 

が続いて終わりにちょうど私が文字列に各行を壊すことをお勧めあなたの文字列(var total_string = string + string1 + string2 +...b)を連結してから言って、

var labelBoston = [ 
    new GInfoWindowTab("Center", total_string); 
]; 
関連する問題