2016-11-25 27 views
0

是否可以在text =「something」中創建換行符?例如,我希望早上的問候語是:早安! (換行符)今天過得如何?javascript中的換行符

我嘗試過\ n像這樣:text =「早上好!」 +「\ n」+「今天好嗎?」

有什麼建議嗎?

這裏是我的代碼:

var myDate = new Date(); 
 
var text = ""; 
 

 
/* hour is before noon */ 
 
if (myDate.getHours() < 12) { 
 
    text = "Good Morning!"; 
 
} 
 

 
/* Hour is from noon to 5pm (actually to 5:59 pm) */ 
 
else if (myDate.getHours() >= 12 && myDate.getHours() <= 17) { 
 
    text = "Good Afternoon!"; 
 
} 
 

 
/* the hour is after 5pm, so it is between 6pm and midnight */ 
 
else if (myDate.getHours() > 17 && myDate.getHours() <= 24) { 
 
    text = "Good Evening!"; 
 
} 
 

 
/* the hour is not between 0 and 24, so something is wrong */ 
 
else { 
 
    text = "Hallo!"; 
 
} 
 

 
$("#rowheader h1").text(text);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="rowheader"> 
 
    This is where the magic happens 
 
    <h1></h1> 
 

 
</div>

+3

類似的問題已經在這裏找到答案: http://stackoverflow.com/questions/4535888/jquery-text-and-newlines –

回答

1

可以插入在文本中的變量的單個換行並使用的.html()代替的.text()是這樣的:

var myDate = new Date(); 
 
var text = ""; 
 

 
/* hour is before noon */ 
 
if (myDate.getHours() < 12) { 
 
    text = "Good Morning! <br /> How are you today?"; 
 
} 
 

 
/* Hour is from noon to 5pm (actually to 5:59 pm) */ 
 
else if (myDate.getHours() >= 12 && myDate.getHours() <= 17) { 
 
    text = "Good Afternoon! <br /> How are you today?"; 
 
} 
 

 
/* the hour is after 5pm, so it is between 6pm and midnight */ 
 
else if (myDate.getHours() > 17 && myDate.getHours() <= 24) { 
 
    text = "Good Evening! <br /> How are you today?"; 
 
} 
 

 
/* the hour is not between 0 and 24, so something is wrong */ 
 
else { 
 
    text = "Hallo!"; 
 
} 
 

 
$("#rowheader h1").html(text);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div id="rowheader"> 
 
    <h1></h1> 
 
</div>

+1

完美的作品。謝謝! – suonpera

+0

不客氣:-) – Cecilia

0

它是一個選項,讓你有更多的標籤工作?

<div id="rowheader"> 
    <h1 id="headerline1"></h1> 
    <h1 id="headerline2">How are you today?</h1> 
</div> 

您可以使用CSS來調整外觀和感覺如果需要的話......在你的JS代碼,然後:

$("#headerline1").text(text); 
0

Working Demo

應用一些CSS技巧。

樣品h1標籤:

<h1 id="yourheader" class="test"> 

</h1> 

腳本:

var value = "good morning!" + "\n" + "How are you today?"; 
$('#yourheader').html(value); 

CSS:

.test{ 
    white-space:pre-wrap; 
} 
0

我想你可以使用jQuery的html方法,而不是像這樣:

text += "<br/>"; 
text += "How are you today?"; 

$("#rowheader h1").html(text); 
0

爲了直觀地顯示一個啉在html文檔中打破,您需要部署元素<br>

因此,雖然原來的腳本創建textContenth1它可能是一個更好的方法來創建innerHTMLh1,而不是:

var heading = document.getElementById('rowheader').getElementsByTagName('h1')[0]; 
 
var myDate = new Date(); 
 
var html = ''; 
 

 
/* hour is before noon */ 
 
if (myDate.getHours() < 12) { 
 
    html = 'Good Morning!'; 
 
} 
 

 
/* Hour is from noon to 5pm (actually to 5:59 pm) */ 
 
else if (myDate.getHours() >= 12 && myDate.getHours() <= 17) { 
 
    html = 'Good Afternoon!'; 
 
} 
 

 
/* the hour is after 5pm, so it is between 6pm and midnight */ 
 
else if (myDate.getHours() > 17 && myDate.getHours() <= 24) { 
 
    html = 'Good Evening!'; 
 
} 
 

 
/* the hour is not between 0 and 24, so something is wrong */ 
 
else { 
 
    html = 'Hallo!'; 
 
} 
 

 
html += '<br />How are you today?'; 
 

 
heading.innerHTML = html;
<div id="rowheader"> 
 
    This is where the magic happens 
 
    <h1></h1> 
 

 
</div>