问题
So I am doing an assignment and I am incredibly stuck at the moment with a few steps to go until the finish. Here is the code I have thus far and what I need to do is first off is put the "var week" on a new line and ive tried many ways of /n but ever time it either breaks the script or does nothing. Also the other thing I need to do is create a loop that will display each day of the month (example, Mon 1, Tue 2, Wed 3, ect until all the days of April are displayed) and for the life of me I can not figure out a way to do this, so can anyone help me out with a few hints on how to put this together? And yes I have searched for quite a while on how to create a space and none of the results have helped.
edit: Ive updated the code i finally figured out how to display the days but I do not know how to display the numbers after them, and Ive figured out how to do the line breaks as well, sorry for the huge amount of text.
<script>
var monthArray=["January","February","March","April","May","June","July","August","September","October","November","December"];
var weekDaysArray=["Sun","Mon","Tues","Wed","Thurs","Fri","Sat"];
//The comments below are for the current date but I was not sure if we were supposed to show that
//var todaysDate=new Date();
//document.write(todaysDate);
var currMon=monthArray[3];
document.write(currMon);
document.write("\n");
var d = new Date();
var currYr = d.getFullYear();
document.write(currYr +"<br>");
var week="Week 1 of April.";
document.write(week +"<br>");
for (var i=0;i<weekDaysArray.length;i++)
{
document.write(weekDaysArray[i] + "<br>");
}
</script>
回答1:
In HTML, whitespace is condensed. If you write a newline in your source, it doesn't show in the HTML (unless you use CSS or <pre>
). The same applies to newlines generated by JavaScript.
Try document.write("<br>");
来源:https://stackoverflow.com/questions/15962639/javascript-code-new-line-loops