初始化插件后如何更改默认值

时间:2015-08-14 23:36:30

标签: javascript jquery

所以我想尝试使用下面的代码创建一个插件。

基本上,该插件是一个位于 calendar.js

内的日历

我无法弄清楚我如何创建下一个和上一个按钮,以便在月份和天数之间进行更改?

这是插件代码

var Calendar = Calendar || (function(){


  return {
    init: function(args) {
      _args = args; //arguments array

      var Months = new Array( "January", "February", "March", "April", "May", "June" , "July" , "August" ,"September" ,
                              "October" , "November" , "December" ) ;

      var daysMonth = new Array(31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ) ;

      var theDays   = new Array( "Sunday" , "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday") ;

      var leapYear = function (year){
        if (year % 4 == 0 || (year % 100 == 0 && year % 400 == 0)){
          length = 29;
        }else{
          length = 28;
        }
        return length;
      }

      var today = new Date(); // whole date including the hour
      var date = today.getDate(); // returns the date only e.g 15 (of August)
      var month = Months[today.getMonth()]; // returns month name after it gets month number
      var year = today.getFullYear(); // returns year

      // check february if it has 28 or 29 days
      if (today.getMonth() == 1){
        daysMonth[1] = leapYear(year);
      }

      // get the day place in the week
      today.setDate(1);
      var dayOfWeek = today.getDay();

      // get total cells number
      var howManyCells = daysMonth[today.getMonth()] + dayOfWeek ;

      function populateCalendar() {

       var content = "<TR>" ;

       for (var i= 1 ; i <= howManyCells ; i++) {
              if (i <= dayOfWeek  ) {
                 // cells prior to first day occurrence
                 content += "<TD><pre> </pre></TD>"
              }
              else {
                 // enter date number
                 content += "<TD align=center>" + (i - (dayOfWeek) ) + "</TD>"
              }
              // must start new row after each week
              if ( i % 7 == 0 &&  i != howManyCells ) {
                 content += "</TR><TR>"
              }

           }

           return content ;
        }
      var content = "<center><table border=3>" ;
       content     += "<tr><th COLSPAN=7>" + month + " " + ( today.getFullYear() ) + "</th></tr>"
       content     += "<tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr>"

       var content1 = populateCalendar() ;

       content     += content1 ;
       content += "</table></center>" ;

       document.write(content);
    }
  }
}());

并且我将其初始化并将其传递给如下所示的数组。里面有默认值,但可以更改。

Calendar.init(["test"]);

0 个答案:

没有答案