是否可以在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 –