如何摆脱Javascript“Undefined”

时间:2011-10-20 20:55:33

标签: javascript html css

我正在尝试制作一个用Javascript更新的日历,但是“未定义”显示在底部,我看不出任何错误。

这是javascript:

function calendar(month){
var s= new Date();

var year=s.getFullYear();
var wday= s.getDay();
var mday =s.getDate();


var days = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
 document.write(' <table border=\"1\" bordercolor=\"black\" cellpadding=\"1\" cellspacing=\"0\" width=\"220px\"><h6 style="size:.5em;"><tr>\n');


 for (i=0; i<(days.length); i++) {
     document.write(' <td style=\"background-color: #CCCCCC; text-align: center; width: 33px;padding:2px; \"> <strong> ' + days[i] + ' </strong></td> ');
 }
 var day=1;
 b=6;

 for ( count = 0; count < (b*7); count++) {

         var start= new Date(year,month,day,0,0,0,0);

         var dayarray = new Array();     


          dayarray['mon'] = start.getMonth();// month
          dayarray['yr'] = start.getYear();// year
          dayarray['wday'] = start.getDay();//day of the week
          dayarray['mday'] = start.getDate();// day of the month

         if ((count % 7) == 0) {

                                document.write(' </tr><tr>\n');
                        }

                if ((count < (dayarray['wday'])) || (dayarray['mon']!=month)) {

                        document.write ('<td style=\"background-color: white; text-align: center; width: 33px;padding:2px; \" >&nbsp;</td>\n');

                } else {
                        document.write(' <td style=\"background-color: white; text-align: center; width: 33px;padding:2px; \">' + dayarray['mday'] + '   </td>\n ');
                         day +=1;

                }

}
document.write(' </tr></h6></table>');
}
function next(c){
    document.getElementById(c).style.visibility="visible";
    document.getElementById((c-1)).style.visibility="hidden";

}

和Html:

<div style="display: block;">
                <img src="style/rightArrow.jpg" onclick="var it=12;next(it);it++;"/>


        <br/>
        <div id="calendar" style="margin:auto;width:220px;height:182px;" >
            <div id="11" style="position:absolute;left:10px;top:400px;visibility: visible;">
    <script type="text/javascript">var ha=0;d=new Date();document.write(calendar(d.getMonth()+ha));</script>
    </div>

    <div id="12" style="position:absolute;left:10px;top:400px;visibility:hidden;">
    <script type="text/javascript">var ha=1;d=new Date();document.write(calendar(d.getMonth()+ha));</script>
    </div>

    <div id="13" style="position:absolute;left:10px;top:400px;visibility:hidden;"> 
    <script type="text/javascript">var ha=2;d=new Date();document.write(calendar(d.getMonth()+ha));</script>

    </div>
    </div>
    </div>

第二个问题:

为什么日历只提前一个月?

1 个答案:

答案 0 :(得分:2)

问题在于你绕过每个函数调用的document.write()

<div id="calendar" style="margin:auto;width:220px;height:182px;" >
            <div id="11" style="position:absolute;left:10px;top:400px;visibility: visible;">
    <script type="text/javascript">var ha=0;d=new Date();calendar(d.getMonth()+ha);</script>
    </div>

从日历调用周围删除它们,你不会得到未定义的。 http://jsfiddle.net/qXARb/2/