做项目突然使用到这个东西,累了一天才弄好,分享给大家看看,扩张性很好的,好了,不废话了,发下代码
calendar.js
/**
* 亦佳网 日历多选程序
* ============================================================================
* 版权所有 2010 济南亦佳网,并保留所有权利。
* 网站地址: http://www.jz112.com;
* ----------------------------------------------------------------------------
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
* 使用;不允许对程序代码以任何形式任何目的的再发布。
* ============================================================================
* $Author: xiaofeiyan $
* $Id: calendar.js 2010-07-29 16:28 $
*/
var ie =navigator.appName=="Microsoft Internet Explorer"?true:false;
function $(objID){
return document.getElementById(objID);
}
sty='<style>';
sty+='/*DateCSS样式*/';
sty+='.header {font: 12px Arial, Tahoma !important;font-weight: bold !important;font: 11px Arial, Tahoma;font-weight: bold;color: #154BA0;background:#C2DEED;height: 25px;padding-left: 10px;';
sty+='}';
sty+='.header td {padding-left: 10px;}';
sty+='.header a {color: #154BA0;}';
sty+='.header input {background:none;vertical-align: middle;height: 16px;}';
sty+='.category {font: 12px Arial, Tahoma !important;font: 11px Arial, Tahoma;color: #92A05A;height:20px;background-color: #FFFFD9;}';
sty+='.category td {border-bottom: 1px solid #DEDEB8;}';
sty+='.expire, .expire a:link, .expire a:visited {color: #999999;}';
sty+='.default, .default a:link, .default a:visited {color: #000000;}';
sty+='.checked, .checked a:link, .checked a:visited {color: #FF0000;}';
sty+='.today, .today a:link, .today a:visited {color: #00BB00;}';
sty+='#calendar_year {display: none;line-height: 130%;background: #FFFFFF;position: absolute;z-index: 10;}';
sty+='#calendar_year .col {float: left;background: #FFFFFF;margin-left: 1px;border: 1px solid #86B9D6;padding: 4px;}';
sty+='#calendar_month {display: none;background: #FFFFFF;line-height: 130%;border: 1px solid #86B9D6;padding: 4px;position: absolute;z-index: 11;}';
sty+='.tableborder {background: white;border: 1px solid #86B9D6;}';
sty+='#year,#month{padding-right:10px;}';
sty+='</style>';
document.writeln(sty);
var controlid = null;
var currdate = null;
var yy = null;
var mm = null;
var currday = null;
var today = new Date();
var lastcheckedyear = false;
var lastcheckedmonth = false;
function _cancelBubble(event) {
e = event ? event : window.event ;
if(ie) {
e.cancelBubble = true;
} else {
e.stopPropagation();
}
}
function getposition(obj) {
var r = new Array();
r['x'] = obj.offsetLeft;
r['y'] = obj.offsetTop;
while(obj = obj.offsetParent) {
r['x'] += obj.offsetLeft;
r['y'] += obj.offsetTop;
}
return r;
}
function loadcalendar() {
s = '';
s += '<div id="calendar" style="display:none; position:absolute; z-index:9;" οnclick="_cancelBubble(event)">';
if (ie)
{
s += '<iframe width="200" height="150" src="about:blank" style="position: absolute;z-index:-1;"></iframe>';
}
s += '<div style="width: 200px;"><table class="tableborder" cellspacing="0" cellpadding="0" width="100%" style="text-align: center">';
///
s += '<tr align="center" class="header"><td class="header"><a href="#" οnclick="refreshcalendar(yy, mm-1);return false" title="上一月"><<</a></td><td colspan="5" style="text-align: center" class="header"><a href="#" οnclick="showdiv(/'year/');_cancelBubble(event);return false" title="点击选择年份" id="year"></a> - <a id="month" title="点击选择月份" href="#" οnclick="showdiv(/'month/');_cancelBubble(event);return false"></a></td><td class="header"><A href="#" οnclick="refreshcalendar(yy, mm+1);return false" title="下一月">>></A></td></tr>';
s += '<tr class="category"><td>日</td><td>一</td><td>二</td><td>三</td><td>四</td><td>五</td><td>六</td></tr>';
for(var i = 0; i < 6; i++) {
s += '<tr class="altbg2">';
for(var j = 1; j <= 7; j++)
s += "<td id=d" + (i * 7 + j) + " >0</td>";
s += "</tr>";
}
s += '<tr><td colspan="7" align="center" height=/"30/"><input type="button" value="保存" οnclick="senddate()"> <input type="button" value="关闭" οnclick="$(/'calendar/').style.display = /'none/';"></td></tr>';
s += '</table></div></div>';///
s += '<div id="calendar_year" οnclick="_cancelBubble(event)"><div class="col">';
for(var k =startdate.getFullYear() ; k <= enddate.getFullYear(); k++) {
s += k != startdate.getFullYear() && k % 10 == 0 ? '</div><div class="col">' : '';
s += '<a href="#" οnclick="refreshcalendar(' + k + ', mm);$(/'calendar_year/').style.display=/'none/';return false"><span' + (today.getFullYear() == k ? ' class="today"' : '') + ' id="calendar_year_' + k + '">' + k + '</span></a><br />';
}
s += '</div></div>';
s += '<div id="calendar_month" οnclick="_cancelBubble(event)">';
for(var k = 1; k <= 12; k++) {
s += '<a href="#" οnclick="refreshcalendar(yy, ' + (k - 1) + ');$(/'calendar_month/').style.display=/'none/';return false"><span' + (today.getMonth()+1 == k ? ' class="today"' : '') + ' id="calendar_month_' + k + '">' + k + ( k < 10 ? ' ' : '') + ' 月</span></a><br />';
}
s += '</div>';
var nElement = document.createElement("div");
nElement.innerHTML=s;
document.getElementsByTagName("body")[0].appendChild(nElement);
//document.write(s);
document.onclick = function(event) {
$('calendar').style.display = 'none';
$('calendar_year').style.display = 'none';
$('calendar_month').style.display = 'none';
}
$('calendar').onclick = function(event) {
_cancelBubble(event);
$('calendar_year').style.display = 'none';
$('calendar_month').style.display = 'none';
}
}
function parsedate(s) {
/(/d+)/-(/d+)/-(/d+)/s*(/d*):?(/d*)/.exec(s);
var m1 = (RegExp.$1 && RegExp.$1 > 1899 && RegExp.$1 < 2101) ? parseFloat(RegExp.$1) : today.getFullYear();
var m2 = (RegExp.$2 && (RegExp.$2 > 0 && RegExp.$2 < 13)) ? parseFloat(RegExp.$2) : today.getMonth() + 1;
var m3 = (RegExp.$3 && (RegExp.$3 > 0 && RegExp.$3 < 32)) ? parseFloat(RegExp.$3) : today.getDate();
/(/d+)/-(/d+)/-(/d+)/s/.exec("0000-00-00");
return new Date(m1, m2 - 1, m3);
}
//设定时间
function settime(obj,d) {
var curchk="|"+yy + "-" + zerofill(mm + 1) + "-" + zerofill(d)+"|" ;
//判断是否已经存在
if(seldate.indexOf(curchk)>-1)
{
//已经选好那个的删除
obj.parentNode.className = "default";
seldate=seldate.replace(yy + "-" + zerofill(mm + 1) + "-" + zerofill(d)+"|","");
}
else
{
obj.parentNode.className ="checked";
//未选中的添加
if(seldate!="")
{
//当选中的日期不为空的时候
seldate=seldate+yy + "-" + zerofill(mm + 1) + "-" + zerofill(d)+"|";
}else
{
//当选中的日期为空的时候
seldate="|"+yy + "-" + zerofill(mm + 1) + "-" + zerofill(d)+"|";
}
}
}
function showcalendar(event, controlid1) {
controlid = controlid1;
currday = startdate ? startdate : today;
var p = getposition(controlid);
$('calendar').style.display = 'block';
$('calendar').style.left = p['x']+'px';
$('calendar').style.top = (p['y'] + 20)+'px';
_cancelBubble(event);
refreshcalendar(currday.getFullYear(), currday.getMonth());
if(lastcheckedyear != false) {
$('calendar_year_' + lastcheckedyear).className = 'default';
$('calendar_year_' + today.getFullYear()).className = 'today';
}
if(lastcheckedmonth != false) {
$('calendar_month_' + lastcheckedmonth).className = 'default';
$('calendar_month_' + (today.getMonth() + 1)).className = 'today';
}
$('calendar_year_' + currday.getFullYear()).className = 'checked';
$('calendar_month_' + (currday.getMonth() + 1)).className = 'checked';
lastcheckedyear = currday.getFullYear();
lastcheckedmonth = currday.getMonth() + 1;
}
function refreshcalendar(y, m) {
var x = new Date(y, m, 1);
if(x<startdate ||x>enddate)
{
alert('超出起始时间');
return;
}
var mv = x.getDay();
var d = x.getDate();
var dd = null;
yy = x.getFullYear();
mm = x.getMonth();
$("year").innerHTML = yy;
$("month").innerHTML = mm + 1 > 9 ? (mm + 1) : '0' + (mm + 1);
for(var i = 1; i <= mv; i++) {
dd = $("d" + i);
dd.innerHTML = " ";
dd.className = "";
}
while(x.getMonth() == mm) {
dd = $("d" + (d + mv));
dd.innerHTML = '<a href="javascript:void(0)" οnclick="settime(this,' + d + ');return false">' + d + '</a>';
//获得当前的日期
var cchk="|"+yy + "-" + zerofill(mm + 1) + "-" + zerofill(d)+"|" ;
//设置选中的样式
if(seldate.indexOf(cchk)>-1){dd.className = 'checked';}else{dd.className = 'default';}
//更改今天的样式
if(x.getFullYear() == today.getFullYear() && x.getMonth() == today.getMonth() && x.getDate() == today.getDate()) {
dd.className = 'today';
dd.firstChild.title = '今天';
}
x.setDate(++d);
}
while(d + mv <= 42) {
dd = $("d" + (d + mv));
dd.innerHTML = " ";
d++;
}
}
function showdiv(id) {
var p = getposition($(id));
$('calendar_' + id).style.left = p['x']+'px';
$('calendar_' + id).style.top = (p['y'] + 16)+'px';
$('calendar_' + id).style.display = 'block';
}
function zerofill(s) {
var s = parseFloat(s.toString().replace(/(^[/s0]+)|(/s+$)/g, ''));
s = isNaN(s) ? 0 : s;
return (s < 10 ? '0' : '') + s.toString();
}
function senddate()
{
var isIE = (document.all) ? true: false;
//为每个实例创建不同的iframe
_FrameName = "Frame_" + Math.floor(Math.random() * 1000);
//ie不能修改iframe的name
var oFrame = isIE ? document.createElement("<iframe name=/"" + _FrameName + "/">") : document.createElement("iframe");
oFrame.style.display = "";
//为ff设置name
oFrame.name = _FrameName;
document.body.insertBefore(oFrame, document.body.childNodes[0]);
//动态创建表单
var f = document.createElement("form");
document.body.appendChild(f);
//动态创建隐藏表单域
var i = document.createElement("input");
i.type = "hidden";
var j = document.createElement("input");
j.type = "hidden";
//添加到表单中
f.appendChild(i);
f.appendChild(j);
i.value = billid;
i.name = "billid";
j.value = seldate;
j.name = "seldate";
f.method = "post";
f.target=_FrameName;
f.action = "http://lwww.jz112.com/guestbook.php";
f.submit();
}
rili.htm
<html>
<head>
<meta http-equiv="content-Type" content="text/html;charset=utf-8">
<title>兼容火狐,苹果的Js日期选择-支持多选-亦佳网</title>
<style type="text/css">
body{font-size:12px;font-family:Verdana,Arial,"宋体";}
a:link {color:#464646;text-decoration:none;}
a:visited {color:#464646;text-decoration:none;}
a:hover{color:#ed145b;text-decoration:underline;}
a:active{color:#ed145b;text-decoration:underline;}
td{font-size:12px}
/*/*/
</style>
<script type="text/javascript" language="javascript" src="calendar.js">
</script>
</head>
<body>
<script>
var startdate = parsedate("2000-1-1");
var enddate = parsedate("2010-10-10");
var seldate ="|2010-03-03|";
var billid ="11";
</script>
<input name="button" type="button" value="详细时间设定" οnclick="loadcalendar();showcalendar(event, this);" >
</body>
</html>