调用:new Calendar("calTable","date","selectYear","selectMonth",2000,2020); 前四个参数都是一些ID,最后两个,前一个是日历的起始年份,后一个是日历的结束年份,若不写,默认当年为结束年份
首先看一下效果:
单击选中的效果是天蓝色:代码中设置了可以选中的时间范围,白色的为可以操作选中的范围:,灰色为不可以操作的范围:
全选的效果:
单击保存,弹出选中的时间:
废话不多说,来看一下代码,因为做项目需要,其中加入了bootstrap2.0,一些控件是用bootstrap的。
<!DOCTYPE html>
<html lang="zh-CN" manifest="cache.manifest">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>日历</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<style type="text/css">
*{ margin:0; padding:0;}
.red{ color:red;}
.date{ cursor:pointer;}
.today{ background:#F90; font-weight:bold;cursor:pointer;}
#calendar{ width:400px; margin:50px auto; }
#date{ text-align:center; border:1px #ccc solid; border-bottom:0;}
#date a{ display:inline-block; width:18px; height:20px; background-position:center -20px; vertical-align:middle; cursor:pointer;}
#date #selectDate{ width:90px;margin-top:5px;}
#preYear{ background:url(img/preYear.gif);}
#preMonth{ background:url(img/preMonth.gif);}
#nextMonth{ background:url(img/nextMonth.gif);}
#nextYear{ background:url(img/nextYear.gif);}
#calTable{ width:100%; border-collapse:collapse;}
#calTable th,#calTable td{ width:70px; height:40px; border:1px #ccc solid; text-align:center;}
#calTable tbody{ font-family:Georgia, "Times New Roman", Times, serif;}
.td_on{ background:#CFE6FE;}
</style>
</head>
<body>
<div id="calendar">
<div id="date">
<a id="preMonth" title="上一年"></a>
<a id="preYear" title="上一月"></a>
<span id="selectDate" class="help-inline">
<select id="selectYear" class="input-small">
</select>
</span>
<span id="selectDate" class="help-inline">
<select id="selectMonth" class="input-small">
<option value="1">1月</option>
<option value="2">2月</option>
<option value="3">3月</option>
<option value="4">4月</option>
<option value="5">5月</option>
<option value="6">6月</option>
<option value="7">7月</option>
<option value="8">8月</option>
<option value="9">9月</option>
<option value="10">10月</option>
<option value="11">11月</option>
<option value="12">12月</option>
</select>
</span>
<a id="nextYear" title="下一月"></a>
<a id="nextMonth" title="下一年"></a>
</div>
<table id="calTable">
<thead>
<tr>
<th class="red">日</th>
<th>一</th>
<th>二</th>
<th>三</th>
<th>四</th>
<th>五</th>
<th class="red">六</th>
</tr>
</thead>
<tbody>
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
</tr>
<tr>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td>
</tr>
</tbody>
</table>
<label class="checkbox inline">
<input type="checkbox" id="SelectAll" onClick="selectAll(this);"> 全选
</label>
<button type="button" class="btn btn-default pull-right" style="margin:5px" οnclick="setTable()">保存</button>
</div>
<script type="text/javascript">
var objjson={};
function setTable(){
var tyear=selectValue($("#selectYear"));
var tmonth=selectValue($("#selectMonth"));
$("td.td_on").each(function (){
var tday = $(this).html();
if($(this).html()<10){
tday = "0"+tday;
}
var time = tyear +"-"+tmonth+"-"+tday;
alert(time);
});
}
var selectValue = function(selectObj){
var selectList=$('option',selectObj);
for(var i=0,len=selectList.length;i<len;i++){
var _option=selectList[i];
if(_option.selected){
return parseInt(_option.value);
}
}
}
function selectAll(_this){
if($(_this).hasClass("currentcheck")){//取消全选
$("td.date").removeClass("td_on");
}else{//全选
$("td.date").addClass("td_on");
}
$(_this).toggleClass("currentcheck");
}
var startdate = "2013-11-07";
var enddate = "2013-12-21";
var doc=document;
function Calendar(){
this.init.apply(this,arguments);
}
Calendar.prototype={
init:function(tableId,dateId,selectY,selectM,startYear,endYear){
var table=doc.getElementById(tableId);
var dateObj=doc.getElementById(dateId);
var selectY=doc.getElementById(selectY);
var selectM=doc.getElementById(selectM);
this._setSelectYear(selectY,startYear,endYear);
this._setTodayDate(table,selectY,selectM);
this._selectChange(table,selectY,selectM);
this._clickBtn(table,dateObj,selectY,selectM,startYear,endYear);
},
//设置年份
_setSelectYear:function(selectY,startYear,endYear){
var html="";
var date=new Date();
if(!endYear){
var endYear=date.getFullYear();
}else{
var endYear=endYear;
}
for(var i=startYear;i<=endYear;i++){
var _option=document.createElement('option');
selectY.appendChild(_option);
_option.value=i;
_option.innerHTML=i;
}
},
//设置当天的时间
_setTodayDate:function(table,selectY,selectM){
var _this=this;
var date=new Date();
var year=date.getFullYear(),month=date.getMonth(),_date=date.getDate(),day=date.getDay();
var n=parseInt(_date/7);
var l=n%7;
if(l>=day){
var todayTd=day+7*l;
}else{
var todayTd=day+7*(l+1);
}
var startTd=todayTd-_date+1;
var monthDays=this._getMonthDays(year,month);
var td=table.getElementsByTagName('td');
this._setSelectValue(selectY,year);
this._setSelectValue(selectM,month+1);
for(i=startTd,len=td.length;i<len;i++){
var _td=td[i];
var j=i-startTd+1
_td.innerHTML=j;
var tday = j;
if(j<10){
tday = "0"+j;
}
var currentdate=year+"-"+(month+1)+"-"+tday;
if(currentdate>=startdate&¤tdate<=enddate){
_td.className="date";
_td.style.background="";
_this._clickOn(_td);
}else{
_td.style.background="#EEEEEE";
}
if(j>=monthDays){
break;
}
}
},
//鼠标单击日期
_clickOn:function(obj){
obj.οnclick=function(){
if(this.innerHTML&&$(this).hasClass("date")){
if($(this).hasClass("td_on")){
$(this).removeClass("td_on");
$("#SelectAll").removeClass("currentcheck");
$("#SelectAll").attr("checked",false);
}else{
$(this).addClass("td_on");
}
}
}
},
//下拉菜单选择日期
_selectChange:function(table,selectY,selectM){
var _this=this;
selectY.οnchange=function(){
var year=_this._getSelectValue(selectY);
var month=_this._getSelectValue(selectM)-1;
_this._showCalendar(table,year,month);
}
selectM.οnchange=function(){
var year=_this._getSelectValue(selectY);
var month=_this._getSelectValue(selectM)-1;;
_this._showCalendar(table,year,month);
}
},
//获取下拉菜单的默认值
_getSelectValue:function(selectObj){
var selectList=selectObj.getElementsByTagName('option');
for(var i=0,len=selectList.length;i<len;i++){
var _option=selectList[i];
if(_option.selected){
return parseInt(_option.value);
}
}
},
//设置下拉菜单默认值
_setSelectValue:function(selectObj,value){
var selectList=selectObj.getElementsByTagName('option');
for(var i=0,len=selectList.length;i<len;i++){
var _option=selectList[i];
if(parseInt(_option.value)==value){
_option.selected=true;
break;
}
}
},
_clickBtn:function(table,dateObj,selectY,selectM,startYear,endYear){
var _this=this,year=0;
var btn=dateObj.getElementsByTagName('a');
btn[0].οnclick=function(){
year=_this._getSelectValue(selectY)-1;
var month=_this._getSelectValue(selectM);
if(!isYearOver(year)){
return;
}
_this._setSelectValue(selectY,year);
_this._setSelectValue(selectM,month);
_this._showCalendar(table,year,month-1);
}
btn[1].οnclick=function(){
year=_this._getSelectValue(selectY);
var month=_this._getSelectValue(selectM)-1;
if(month<=0){
month=12;
year--;
}
if(!isYearOver(year)){
return;
}
_this._setSelectValue(selectM,month);
_this._setSelectValue(selectY,year);
_this._showCalendar(table,year,month-1);
}
btn[2].οnclick=function(){
year=_this._getSelectValue(selectY);
var month=_this._getSelectValue(selectM)+1;
if(month>12){
month=1;
year++;
}
if(!isYearOver(year)){
return;
}
_this._setSelectValue(selectM,month);
_this._setSelectValue(selectY,year);
_this._showCalendar(table,year,month-1);
}
btn[3].οnclick=function(){
year=_this._getSelectValue(selectY)+1;
var month=_this._getSelectValue(selectM);
if(!isYearOver(year)){
return;
}
_this._setSelectValue(selectM,month);
_this._setSelectValue(selectY,year);
_this._showCalendar(table,year,month-1);
}
function isYearOver(year){
var date=new Date();
var _endYear=endYear?endYear:date.getFullYear();
if(year>_endYear||year<startYear){
alert("超出日期范围");
return false;;
}else{
return true;
}
}
},
//显示日历
_showCalendar:function(table,year,month){
$("#SelectAll").removeClass("currentcheck");
$("#SelectAll").attr("checked",false);
var date=new Date();
var _year=date.getFullYear();
var _month=date.getMonth();
var _date=date.getDate();
date.setYear(year);
date.setMonth(month);
date.setDate(1);
var day=date.getDay();
var _this=this;
var monthDays=this._getMonthDays(year,month);
var td=table.getElementsByTagName('td');
for(var k=0;k<td.length;k++){
td[k].innerHTML="";
td[k].className="";
td[k].style.background="";
}
for(var i=day,len=td.length;i<len;i++){
var _td=td[i];
var j=i-day+1;
_td.innerHTML=j;
var tday = j;
if(j<10){
tday = "0"+j;
}
var currentdate=year+"-"+(month+1)+"-"+tday;
if(currentdate>=startdate&¤tdate<=enddate){
_td.className="date";
_td.style.background="";
_this._clickOn(_td);
}else{
_td.style.background="#EEEEEE";
}
if(j>=monthDays){
break;
}
}
},
//返回某个月的天数
_getMonthDays:function(year,month){
var monthAry=[31,28,31,30,31,30,31,31,30,31,30,31];
if(year%400==0){
monthAry[1]=29;
}else{
if(year%4==0&&year%100!=0){
monthAry[1]=29;
}
}
return monthAry[month];
}
}
new Calendar("calTable","date","selectYear","selectMonth",2000,2020);
</script>
<link href="css/bootstrap.min.css" rel="stylesheet" />
</body>
</html>