javascript代码(换行,循环) [英] javascript code (new line, loops)

查看:90
本文介绍了javascript代码(换行,循环)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在做作业,此刻我难以置信地停留了几步,直到完成.这是我到目前为止的代码,首先要做的是将"var week"放在换行符上,并且我尝试了多种/n方式,但是无论何时,它要么破坏脚本,要么不执行任何操作.另外,我需要做的另一件事是创建一个循环,该循环将显示该月的每一天(例如,星期一1,星期二2,星期三,等,直到显示四月的所有日子),并且在我的生命中,我可以不知道如何做到这一点,所以有人可以帮我一些有关如何将其组合在一起的提示吗?是的,我已经搜索了很长时间有关如何创建空间的搜索,但结果都没有帮助.

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更新了代码,我终于弄清楚了如何显示日期,但是我不知道如何在日期后显示数字,Ive也弄清楚了如何进行换行,对不起,文字.

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>

推荐答案

在HTML中,空格被压缩.如果在源代码中编写换行符,则换行符不会显示在HTML中(除非您使用CSS或<pre>).这同样适用于JavaScript生成的换行符.

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.

尝试document.write("<br>");

这篇关于javascript代码(换行,循环)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆