2016-11-25 128 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>