跳跃的鱼
2017-05-05 22:18
请问一下为何我最终是undefined,获取不到数据,结果为Null
(function () { var datepicker = {}; datepicker.getMonthData = function (year,month) {//获取一个月的数据, var ret = []; if (!year || !month){ var today = new Date(); year = today.getFullYear();//获取年份 month = today.getMonth()+1;//获取月份 } var firstDay = new Date(year,month-1,1);//获取当月的第一天 var firstDayWeekDay = firstDay.getDay();//获取第一天是星期几 if ( firstDayWeekDay===0){//判断是否是周日 firstDayWeekDay = 7; } year = firstDay.getFullYear(); month = firstDay.getMonth() + 1; var lastDayOfLastMonth = new Date(year,month-1,0);//获取上个月的最后一天 var lastDateOfLastMonth = lastDayOfLastMonth.getDate(); // console.log(lastDateOfLastMonth); var preMonthDayCount = firstDayWeekDay - 1;// var lastDay = new Date(year,month,0);//本月的最后一天 var lastDate = lastDay.getDate(); for(var i=0;i<7*6;i++){ var date = i + 1 - preMonthDayCount; var showDate = date; var thisMonth = month; if (date<=0){ //上一月 thisMonth = month - 1; showDate = lastDateOfLastMonth + date; }else if(date > lastDate){//下一月 thisMonth = month + 1; showDate = showDate - lastDate; } if(thisMonth === 0) thisMonth = 12; if(thisMonth ===13) thisMonth = 1; ret.push({ month:thisMonth,//月份 date:date, //真实日期 showDate:showDate//显示的日期 }); } return{ year:year, month:month, days:ret } }; window.datepicker = datepicker;//用window定义的变量是全局的变量 })();
main.js
(function () { var datepicker = window.datepicker; datepicker.buildUi = function (year ,month) { //buildUi 为渲染函数 var monthData = datepicker.getMonthData(year,month); var html = '<div class="ui-datepicker-header">'+ '<a href="#" class="ui-datepicker-btn ui-datepicker-prev-btn"><</a>'+ '<a href="#" class="ui-datepicker-btn ui-datepicker-next-btn">></a>'+ '<span class="ui-datepicker-curr-month">'+monthData.year +'-'+monthData.month+'</span>'+ '</div>'+ '<div class="ui-datepicker-body">'+ '<table>'+ '<thead>'+ '<tr>'+ '<th>一</th>'+ '<th>二</th>'+ '<th>三</th>'+ '<th>四</th>'+ '<th>五</th>'+ '<th>六</th>'+ '<th>日</th>'+ '</tr>'+ '</thead>'+ '<tbody>'; for(var i=0;i<monthData.days.length;i++){ var date = monthData.days[i]; if (i%7 ===0){ html +='<tr>'; } html +='<td>'+date.showDate + '</td>'; if (i%7 ===6){ html +='</tr>'; } } html +='</tbody>'+ '</table>'+ '</div>'; }; datepicker.init=function ($dom) { var html = datepicker.buildUi(); $dom.innerHTML = html; }; })();
详细描述一下,哪里不对?
buildUi没返回html,其他看不出来
循环那里应该是var date = MonthDate[i]
DatePicker组件开发
17743 学习 · 64 问题
相似问题