是否可以在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>
类似的问题已经在这里找到答案: http://stackoverflow.com/questions/4535888/jquery-text-and-newlines –