欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

兼容FireFox 的 js 日历 支持时间的获取

程序员文章站 2023-11-08 22:56:40
复制代码 代码如下:var cal; var isfocus=false; //是否为焦点 var pickmode ={ "second":1, "minute":2,...
复制代码 代码如下:

var cal;
var isfocus=false; //是否为焦点
var pickmode ={
"second":1,
"minute":2,
"hour":3,
"day":4,
"month":5,
"year":6 };

var topy=0,leftx=0; //自定义定位偏移量 2007-02-11 由 寒羽枫添加
//选择日期 → 由 寒羽枫 2007-06-10 添加,通过 id 来选日期
function selectdatebyid(id,strformat,x,y)
{
    var obj = document.getelementbyid(id);
    if(obj == null){return false;}
    obj.focus();
    if(obj.onclick != null){obj.onclick();}
    else if(obj.click != null){obj.click();}
    else{selectdate(obj,strformat,x,y)}
}

//选择日期 → 由 寒羽枫 2006-06-25 添加
function selectdate(obj,strformat,x,y)
{

    leftx =(x == null) ? leftx : x;
    topy =(y == null) ? topy : y;//自定义定位偏移量 2007-02-11 由 寒羽枫添加    
    if(document.getelementbyid("containerpanel")==null){initcontainerpanel();}
var date = new date();
var by = date.getfullyear()-50; //最小值 → 50 年前
var ey = date.getfullyear()+50; //最大值 → 50 年后
//cal = new calendar(by, ey,1,strformat); //初始化英文版,0 为中文版
cal = (cal==null) ? new calendar(by, ey, 0) : cal; //不用每次都初始化 2006-12-03 修正
cal.datemode =pickmode["second"]; //复位
if(strformat.indexof('s')< 0) {cal.datemode =pickmode["minute"];}//精度为分
if(strformat.indexof('m')< 0) {cal.datemode =pickmode["hour"];}//精度为时
if(strformat.indexof('h')< 0) {cal.datemode =pickmode["day"];}//精度为日
if(strformat.indexof('d')< 0) {cal.datemode =pickmode["month"];}//精度为月
if(strformat.indexof('m')< 0) {cal.datemode =pickmode["year"];}//精度为年
if(strformat.indexof('y')< 0) {cal.datemode =pickmode["second"];}//默认精度为秒
cal.dateformatstyleold = cal.dateformatstyle;
cal.dateformatstyle = strformat;
cal.show(obj);
}
/**//**//**//**//**//**//**//**
* 返回日期
* @param d the delimiter
* @param p the pattern of your date
2006-06-25 由 寒羽枫 修改为根据用户指定的 style 来确定;
*/
string.prototype.todate = function(style) {
var y = this.substring(style.indexof('y'),style.lastindexof('y')+1);//年
var m = this.substring(style.indexof('m'),style.lastindexof('m')+1);//月
var d = this.substring(style.indexof('d'),style.lastindexof('d')+1);//日
var h = this.substring(style.indexof('h'),style.lastindexof('h')+1);//时
var m = this.substring(style.indexof('m'),style.lastindexof('m')+1);//分
var s = this.substring(style.indexof('s'),style.lastindexof('s')+1);//秒

if(s == null ||s == "" || isnan(s)) {s = new date().getseconds();}
if(m == null ||m == "" || isnan(m)) {m = new date().getminutes();}
if(h == null ||h == "" || isnan(h)) {h = new date().gethours();}
if(d == null ||d == "" || isnan(d)) {d = new date().getdate();}
if(m == null ||m == "" || isnan(m)) {m = new date().getmonth()+1;}
if(y == null ||y == "" || isnan(y)) {y = new date().getfullyear();}
var dt ;
eval ("dt = new date('"+ y+"', '"+(m-1)+"','"+ d+"','"+ h+"','"+ m+"','"+ s +"')");
return dt;
}

/**//**//**//**//**//**//**//**
* 格式化日期
* @param d the delimiter
* @param p the pattern of your date
* @author meizz
*/
date.prototype.format = function(style) {
var o = {
"m+" : this.getmonth() + 1, //month
"d+" : this.getdate(), //day
"h+" : this.gethours(), //hour
"m+" : this.getminutes(), //minute
"s+" : this.getseconds(), //second
"w+" : "天一二三四五六".charat(this.getday()), //week
"q+" : math.floor((this.getmonth() + 3) / 3), //quarter
"s" : this.getmilliseconds() //millisecond
}
if(/(y+)/.test(style)) {
style = style.replace(regexp.$1,
(this.getfullyear() + "").substr(4 - regexp.$1.length));
}
for(var k in o){
if(new regexp("("+ k +")").test(style)){
style = style.replace(regexp.$1,
regexp.$1.length == 1 ? o[k] :
("00" + o[k]).substr(("" + o[k]).length));
}
}
return style;
}

//2007-09-14 由寒羽枫添加返回所选日期
calendar.prototype.returndate = function(dt) {
if (this.datecontrol != null){this.datecontrol.value = dt;}
calendar.hide();
    if(this.datecontrol.onchange == null){return;}    
    //将 onchange 转成其它函数,以免触发验证事件
var ev = this.datecontrol.onchange.tostring(); //找出函数的字串
ev = ev.substring(
            ((ev.indexof("validatoronchange();")> 0) ? ev.indexof("validatoronchange();") + 20 : ev.indexof("{") + 1)
                , ev.lastindexof("}"));//去除验证函数 validatoronchange();
    var fun = new function(ev);        //重新定义函数
    this.datecontrol.changeevent = fun;    
this.datecontrol.changeevent();//触发自定义 changeevent 函数
}

/**//**//**//**//**//**//**//**
* 日历类
* @param beginyear 1990
* @param endyear 2010
* @param lang 0(中文)|1(英语) 可*扩充
* @param dateformatstyle "yyyy-mm-dd";
* @version 2006-04-01
* @author kimsoft (jinqinghua [at] gmail.com)
* @update
*/
function calendar(beginyear, endyear, lang, dateformatstyle) {
this.beginyear = 1950;
this.endyear = 2050;
this.lang = 0; //0(中文) | 1(英文)
this.dateformatstyle = "yyyy-mm-dd hh:mm:ss";

if (beginyear != null && endyear != null){
this.beginyear = beginyear;
this.endyear = endyear;
}
if (lang != null){
this.lang = lang
}

if (dateformatstyle != null){
this.dateformatstyle = dateformatstyle
}

this.datecontrol = null;
this.panel = this.getelementbyid("calendarpanel");
this.container = this.getelementbyid("containerpanel");
this.form = null;

this.date = new date();
this.year = this.date.getfullyear();
this.month = this.date.getmonth();

this.day = this.date.getdate();
this.hour = this.date.gethours();
this.minute = this.date.getminutes();
this.second = this.date.getseconds();

this.colors = {
"cur_word" : "#ffffff", //当日日期文字颜色
"cur_bg" : "#00ff00", //当日日期单元格背影色
"sel_bg" : "#ffcccc", //已被选择的日期单元格背影色 2006-12-03 寒羽枫添加
"sun_word" : "#ff0000", //星期天文字颜色
"sat_word" : "#0000ff", //星期六文字颜色
"td_word_light" : "#333333", //单元格文字颜色
"td_word_dark" : "#cccccc", //单元格文字暗色
"td_bg_out" : "#efefef", //单元格背影色
"td_bg_over" : "#ffcc00", //单元格背影色
"tr_word" : "#ffffff", //日历头文字颜色
"tr_bg" : "#666666", //日历头背影色
"input_border" : "#cccccc", //input控件的边框颜色
"input_bg" : "#efefef" //input控件的背影色
}
/* //2008-01-29 放到了 show ,因为要做 pickmode 判断
this.draw();
this.bindyear();
this.bindmonth();
*/
//this.changeselect();
//this.binddata(); //2006-12-30 由民工.砖家注释
}

/**//**//**//**//**//**//**//**
* 日历类属性(语言包,可*扩展)
*/
calendar.language = {
"year" : [[""], [""]],
"months" : [["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"],
["jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"]
],
"weeks" : [["日","一","二","三","四","五","六"],
["sun","mon","tur","wed","thu","fri","sat"]
],
"hour" : [["时"], ["h"]],
"minute" : [["分"], ["m"]],
"second" : [["秒"], ["s"]],
"clear" : [["清空"], ["cls"]],
"today" : [["今天"], ["today"]],
"picktxt" : [["确定"], ["ok"]],//pickmode 精确到年、月时把今天变成“确定”
"close" : [["关闭"], ["close"]]
}

calendar.prototype.draw = function() {
calendar = this;

var mvary = [];
//mvary[mvary.length] = ' <form name="calendarform" style="margin: 0px;">'; //因 <form> 不能嵌套, 2006-12-01 由寒羽枫改用 div
mvary[mvary.length] = ' <div name="calendarform" style="margin: 0px;">';
mvary[mvary.length] = ' <table width="100%" border="0" cellpadding="0" cellspacing="1" style="font-size:12px;">';
mvary[mvary.length] = ' <tr>';
mvary[mvary.length] = ' <th align="left" width="1%"><input style="border: 1px solid ' + calendar.colors["input_border"] + ';background-color:' + calendar.colors["input_bg"] + ';width:16px;height:20px;';
if(calendar.datemode > pickmode["month"]){mvary[mvary.length] = 'display:none;';}//pickmode 精确到年时隐藏“月”
mvary[mvary.length] ='" name="prevmonth" type="button" id="prevmonth" value="&lt;" /></th>';
mvary[mvary.length] = ' <th align="center" width="98%" nowrap="nowrap"><select name="calendaryear" id="calendaryear" style="font-size:12px;"></select><select name="calendarmonth" id="calendarmonth" style="font-size:12px;';
if(calendar.datemode > pickmode["month"]){mvary[mvary.length] = 'display:none;';}//pickmode 精确到年时隐藏“月”
mvary[mvary.length] = '"></select></th>';
mvary[mvary.length] = ' <th align="right" width="1%"><input style="border: 1px solid ' + calendar.colors["input_border"] + ';background-color:' + calendar.colors["input_bg"] + ';width:16px;height:20px;';
if(calendar.datemode > pickmode["month"]){mvary[mvary.length] = 'display:none;';}//pickmode 精确到年时隐藏“月”
mvary[mvary.length] ='" name="nextmonth" type="button" id="nextmonth" value="&gt;" /></th>';
mvary[mvary.length] = ' </tr>';
mvary[mvary.length] = ' </table>';
mvary[mvary.length] = ' <table id="calendartable" width="100%" style="border:0px solid #cccccc;background-color:#ffffff;font-size:12px;';
if(calendar.datemode >= pickmode["month"]){mvary[mvary.length] = 'display:none;';}//pickmode 精确到年、月时隐藏“天”
mvary[mvary.length] = '" border="0" cellpadding="3" cellspacing="1">';
mvary[mvary.length] = ' <tr>';
for(var i = 0; i < 7; i++) {
mvary[mvary.length] = ' <th style="font-weight:normal;background-color:' + calendar.colors["tr_bg"] + ';color:' + calendar.colors["tr_word"] + ';">' + calendar.language["weeks"][this.lang][i] + '</th>';
}
mvary[mvary.length] = ' </tr>';
for(var i = 0; i < 6;i++){
mvary[mvary.length] = ' <tr align="center">';
for(var j = 0; j < 7; j++) {
if (j == 0){
mvary[mvary.length] = ' <td style="cursor:default;color:' + calendar.colors["sun_word"] + ';"></td>';
} else if(j == 6) {
mvary[mvary.length] = ' <td style="cursor:default;color:' + calendar.colors["sat_word"] + ';"></td>';
} else {
mvary[mvary.length] = ' <td style="cursor:default;"></td>';
}
}
mvary[mvary.length] = ' </tr>';
}

//2009-03-03 添加的代码,放置时间的行
mvary[mvary.length] = ' <tr style="';
if(calendar.datemode >= pickmode["day"]){mvary[mvary.length] = 'display:none;';}//pickmode 精确到时日隐藏“时间”
mvary[mvary.length] = '"><td align="center" colspan="7">';
mvary[mvary.length] = ' <select name="calendarhour" id="calendarhour" style="font-size:12px;"></select>' + calendar.language["hour"][this.lang];
mvary[mvary.length] = '<span style="'
if(calendar.datemode >= pickmode["hour"]){mvary[mvary.length] = 'display:none;';}//pickmode 精确到小时时隐藏“分”
mvary[mvary.length] = '"><select name="calendarminute" id="calendarminute" style="font-size:12px;"></select>' + calendar.language["minute"][this.lang]+'</span>';
mvary[mvary.length] = '<span style="'
if(calendar.datemode >= pickmode["minute"]){mvary[mvary.length] = 'display:none;';}//pickmode 精确到小时、分时隐藏“秒”
mvary[mvary.length] = '"><select name="calendarsecond" id="calendarsecond" style="font-size:12px;"></select>'+ calendar.language["second"][this.lang]+'</span>';
mvary[mvary.length] = ' </td></tr>';

mvary[mvary.length] = ' </table>';
//mvary[mvary.length] = ' </from>';
mvary[mvary.length] = ' <div align="center" style="padding:4px 4px 4px 4px;background-color:' + calendar.colors["input_bg"] + ';">';
mvary[mvary.length] = ' <input name="calendarclear" type="button" id="calendarclear" value="' + calendar.language["clear"][this.lang] + '" style="border: 1px solid ' + calendar.colors["input_border"] + ';background-color:' + calendar.colors["input_bg"] + ';width:40px;height:20px;font-size:12px;cursor:pointer;"/>';
mvary[mvary.length] = ' <input name="calendartoday" type="button" id="calendartoday" value="'
mvary[mvary.length] = (calendar.datemode == pickmode["day"]) ? calendar.language["today"][this.lang] : calendar.language["picktxt"][this.lang];
mvary[mvary.length] = '" style="border: 1px solid ' + calendar.colors["input_border"] + ';background-color:' + calendar.colors["input_bg"] + ';width:60px;height:20px;font-size:12px;cursor:pointer"/>';
mvary[mvary.length] = ' <input name="calendarclose" type="button" id="calendarclose" value="' + calendar.language["close"][this.lang] + '" style="border: 1px solid ' + calendar.colors["input_border"] + ';background-color:' + calendar.colors["input_bg"] + ';width:40px;height:20px;font-size:12px;cursor:pointer"/>';
mvary[mvary.length] = ' </div>';

mvary[mvary.length] = ' </div>';
this.panel.innerhtml = mvary.join("");

/**//**//**//******** 以下代码由寒羽枫 2006-12-01 添加 **********/
var obj = this.getelementbyid("prevmonth");
obj.onclick = function () {calendar.goprevmonth(calendar);}
obj.onblur = function () {calendar.onblur();}
this.prevmonth= obj;

obj = this.getelementbyid("nextmonth");
obj.onclick = function () {calendar.gonextmonth(calendar);}
obj.onblur = function () {calendar.onblur();}
this.nextmonth= obj;

obj = this.getelementbyid("calendarclear");
obj.onclick = function ()
{ calendar.returndate(""); /*calendar.datecontrol.value = "";calendar.hide();*///2007-09-14 由寒羽枫注释
}
this.calendarclear = obj;

obj = this.getelementbyid("calendarclose");
obj.onclick = function () {calendar.hide();}
this.calendarclose = obj;

obj = this.getelementbyid("calendaryear");
obj.onchange = function () {calendar.update(calendar);}
obj.onblur = function () {calendar.onblur();}
this.calendaryear = obj;

obj = this.getelementbyid("calendarmonth");
with(obj)
{
onchange = function () {calendar.update(calendar);}
onblur = function () {calendar.onblur();}
}this.calendarmonth = obj;

obj = this.getelementbyid("calendarhour");
obj.onchange = function () {calendar.hour = this.options[this.selectedindex].value;}
obj.onblur = function () {calendar.onblur();}
this.calendarhour = obj;

obj = this.getelementbyid("calendarminute");
obj.onchange = function () {calendar.minute = this.options[this.selectedindex].value;}
obj.onblur = function () {calendar.onblur();}
this.calendarminute = obj;

obj = this.getelementbyid("calendarsecond");
obj.onchange = function () {calendar.second = this.options[this.selectedindex].value;}
obj.onblur = function () {calendar.onblur();}
this.calendarsecond = obj;

obj = this.getelementbyid("calendartoday");
obj.onclick = function () {
var today = (calendar.datemode != pickmode["day"]) ?
new date(calendar.year,calendar.month,calendar.day,calendar.hour,calendar.minute,calendar.second)
: new date();//2008-01-29
calendar.returndate(today.format(calendar.dateformatstyle));
}
this.calendartoday = obj;
}

//年份下拉框绑定数据
calendar.prototype.bindyear = function() {
var cy = this.calendaryear;//2006-12-01 由寒羽枫修改
cy.length = 0;
for (var i = this.beginyear; i <= this.endyear; i++){
cy.options[cy.length] = new option(i + calendar.language["year"][this.lang], i);
}
}

//月份下拉框绑定数据
calendar.prototype.bindmonth = function() {
var cm = this.calendarmonth;//2006-12-01 由寒羽枫修改
cm.length = 0;
for (var i = 0; i < 12; i++){
cm.options[cm.length] = new option(calendar.language["months"][this.lang][i], i);
}
}

//小时下拉框绑定数据
calendar.prototype.bindhour = function() {
var ch = this.calendarhour;
if(ch.length > 0){return;}//2009-03-03 不需要重新绑定,提高性能
//ch.length = 0;
var h;
for (var i = 0; i < 24; i++){
h = ("00" + i +"").substr(("" + i).length);
ch.options[ch.length] = new option(h, h);
}
}

//分钟下拉框绑定数据
calendar.prototype.bindminute = function() {
var cm = this.calendarminute;
if(cm.length > 0){return;}//2009-03-03 不需要重新绑定,提高性能
//cm.length = 0;
var m;
for (var i = 0; i < 60; i++){
m = ("00" + i +"").substr(("" + i).length);
cm.options[cm.length] = new option(m, m);
}
}

//秒钟下拉框绑定数据
calendar.prototype.bindsecond = function() {
var cs = this.calendarsecond;
if(cs.length > 0){return;}//2009-03-03 不需要重新绑定,提高性能
//cs.length = 0;
var s;
for (var i = 0; i < 60; i++){
s = ("00" + i +"").substr(("" + i).length);
cs.options[cs.length] = new option(s, s);
}
}

//向前一月
calendar.prototype.goprevmonth = function(e){
if (this.year == this.beginyear && this.month == 0){return;}
this.month--;
if (this.month == -1) {
this.year--;
this.month = 11;
}
this.date = new date(this.year, this.month, 1);
this.changeselect();
this.binddata();
}

//向后一月
calendar.prototype.gonextmonth = function(e){
if (this.year == this.endyear && this.month == 11){return;}
this.month++;
if (this.month == 12) {
this.year++;
this.month = 0;
}
this.date = new date(this.year, this.month, 1);
this.changeselect();
this.binddata();
}

//改变select选中状态
calendar.prototype.changeselect = function() {
var cy = this.calendaryear;//2006-12-01 由寒羽枫修改
var cm = this.calendarmonth;
var ch = this.calendarhour;
var cm = this.calendarminute;
var cs = this.calendarsecond;
//2006-12-30 由民工.砖家修改,减少运算次数
cy[this.date.getfullyear()-this.beginyear].selected = true;
cm[this.date.getmonth()].selected =true;

//2009-03-03 添加,初始化时间的值
ch[this.hour].selected =true;
cm[this.minute].selected =true;
cs[this.second].selected =true;
}

//更新年、月
calendar.prototype.update = function (e){
this.year = e.calendaryear.options[e.calendaryear.selectedindex].value;//2006-12-01 由寒羽枫修改
this.month = e.calendarmonth.options[e.calendarmonth.selectedindex].value;
this.date = new date(this.year, this.month, 1);
//this.changeselect();
this.binddata();
}

//绑定数据到月视图
calendar.prototype.binddata = function () {
var calendar = this;
    if(calendar.datemode >= pickmode["month"]){return;}//2008-01-29
// var datearray = this.getmonthviewarray(this.date.getyear(), this.date.getmonth());
//2006-12-30 由民工.砖家修改 在firefox 下年份错误
var datearray = this.getmonthviewarray(this.date.getfullyear(), this.date.getmonth());
var tds = this.getelementbyid("calendartable").getelementsbytagname("td");
for(var i = 0; i < tds.length; i++) {
tds[i].style.backgroundcolor = calendar.colors["td_bg_out"];
tds[i].onclick = function () {return;}
tds[i].onmouseover = function () {return;}
tds[i].onmouseout = function () {return;}
if (i > datearray.length - 1) break;
tds[i].innerhtml = datearray[i];
if (datearray[i] != "&nbsp;"){
tds[i].bgcolortxt = "td_bg_out"; //2009-03-03 保存背景色的class
var cur = new date();
tds[i].istoday = false;
if (cur.getfullyear() == calendar.date.getfullyear() && cur.getmonth() == calendar.date.getmonth() && cur.getdate() == datearray[i]) {
//是今天的单元格
tds[i].style.backgroundcolor = calendar.colors["cur_bg"];
tds[i].bgcolortxt = "cur_bg";
tds[i].istoday = true;
}
if(calendar.datecontrol != null )
{
cur = calendar.datecontrol.value.todate(calendar.dateformatstyle);
if (cur.getfullyear() == calendar.date.getfullyear() && cur.getmonth() == calendar.date.getmonth()&& cur.getdate() == datearray[i]) {
//是已被选中的单元格
calendar.selecteddaytd = tds[i];
tds[i].style.backgroundcolor = calendar.colors["sel_bg"];
tds[i].bgcolortxt = "sel_bg";
}
}
tds[i].onclick = function () {
if(calendar.datemode == pickmode["day"]) //2009-03-03 当选择日期时,点击格子即返回值
{
calendar.returndate(new date(calendar.date.getfullyear(),
calendar.date.getmonth(),
this.innerhtml).format(calendar.dateformatstyle));
}
else
{
if(calendar.selecteddaytd != null) //2009-03-03 清除已选中的背景色
{
calendar.selecteddaytd.style.backgroundcolor =(calendar.selecteddaytd.istoday)? calendar.colors["cur_bg"] : calendar.colors["td_bg_out"];
}
this.style.backgroundcolor = calendar.colors["sel_bg"];
calendar.day = this.innerhtml;
calendar.selecteddaytd = this; //2009-03-03 记录已选中的日子
}
}
tds[i].style.cursor ="pointer"; //2007-08-06 由寒羽枫添加,鼠标变成手指状
tds[i].onmouseover = function () {
this.style.backgroundcolor = calendar.colors["td_bg_over"];
}
tds[i].onmouseout = function () {
if(calendar.selecteddaytd != this) {
this.style.backgroundcolor = calendar.colors[this.bgcolortxt];}
}
tds[i].onblur = function () {calendar.onblur();}
}
}
}

//根据年、月得到月视图数据(数组形式)
calendar.prototype.getmonthviewarray = function (y, m) {
var mvarray = [];
var dayoffirstday = new date(y, m, 1).getday();
var daysofmonth = new date(y, m + 1, 0).getdate();
for (var i = 0; i < 42; i++) {
mvarray[i] = "&nbsp;";
}
for (var i = 0; i < daysofmonth; i++){
mvarray[i + dayoffirstday] = i + 1;
}
return mvarray;
}

//扩展 document.getelementbyid(id) 多浏览器兼容性 from meizz tree source
calendar.prototype.getelementbyid = function(id){
if (typeof(id) != "string" || id == "") return null;
if (document.getelementbyid) return document.getelementbyid(id);
if (document.all) return document.all(id);
try {return eval(id);} catch(e){ return null;}
}

//扩展 object.getelementsbytagname(tagname)
calendar.prototype.getelementsbytagname = function(object, tagname){
if (document.getelementsbytagname) return document.getelementsbytagname(tagname);
if (document.all) return document.all.tags(tagname);
}

//取得html控件绝对位置
calendar.prototype.getabspoint = function (e){
var x = e.offsetleft;
var y = e.offsettop;
while(e = e.offsetparent){
x += e.offsetleft;
y += e.offsettop;
}
return {"x": x, "y": y};
}

//显示日历
calendar.prototype.show = function (dateobj, popcontrol) {
if (dateobj == null){
throw new error("arguments[0] is necessary")
}
this.datecontrol = dateobj;
var now = new date();
this.date = (dateobj.value.length > 0) ? new date(dateobj.value.todate(this.dateformatstyle)) : now.format(this.dateformatstyle).todate(this.dateformatstyle) ;//2008-01-29 寒羽枫添加 → 若为空则根据dateformatstyle初始化日期

if(this.panel.innerhtml==""||cal.dateformatstyleold != cal.dateformatstyle)//2008-01-29 把构造表格放在此处,2009-03-03 若请示的样式改变,则重新初始化
{
    this.draw();
    this.bindyear();
    this.bindmonth();
    this.bindhour();
    this.bindminute();
    this.bindsecond();
}
this.year = this.date.getfullyear();
this.month = this.date.getmonth();
this.day = this.date.getdate();
this.hour = this.date.gethours();
this.minute = this.date.getminutes();
this.second = this.date.getseconds();
this.changeselect();
this.binddata();

if (popcontrol == null){
popcontrol = dateobj;
}
var xy = this.getabspoint(popcontrol);
//this.panel.style.left = xy.x + "px";
//this.panel.style.top = (xy.y + dateobj.offsetheight) + "px";
this.panel.style.left = (xy.x + leftx)+ "px"; //由寒羽枫 2007-02-11 修改 → 加入自定义偏移量
this.panel.style.top = (xy.y + topy + dateobj.offsetheight) + "px";

//由寒羽枫 2006-06-25 修改 → 把 visibility 变为 display,并添加失去焦点的事件 //this.setdisplaystyle("select", "hidden");
//this.panel.style.visibility = "visible";
//this.container.style.visibility = "visible";
this.panel.style.display = "";
this.container.style.display = "";

if( !this.datecontrol.istransevent)
{
    this.datecontrol.istransevent = true;
    /* 已写在返回值的时候 returndate 函数中,去除验证事件的函数
    this.datecontrol.changeevent = this.datecontrol.onchange;//将 onchange 转成其它函数,以免触发验证事件
    this.datecontrol.onchange = function()
    {if(typeof(this.changeevent) =='function'){this.changeevent();}}*/
    if(this.datecontrol.onblur != null){
    this.datecontrol.blurevent = this.datecontrol.onblur;}//2007-09-14 保存主文本框的 onblur ,使其原本的事件不被覆盖
    this.datecontrol.onblur = function()
    {calendar.onblur();if(typeof(this.blurevent) =='function'){this.blurevent();}
    }
}

this.container.onmouseover = function(){isfocus=true;}
this.container.onmouseout = function(){isfocus=false;}
}

//隐藏日历
calendar.prototype.hide = function() {
//this.setdisplaystyle("select", "visible");
//this.panel.style.visibility = "hidden";
//this.container.style.visibility = "hidden";
this.panel.style.display = "none";
this.container.style.display = "none";
isfocus=false;
}

//焦点转移时隐藏日历 → 由寒羽枫 2006-06-25 添加
calendar.prototype.onblur = function() {
if(!isfocus){this.hide();}
}

//以下由寒羽枫 2007-07-26 修改 → 确保日历容器节点在 body 最后,否则 firefox 中不能出现在最上方
function initcontainerpanel() //初始化容器
{
    var str = '<div id="calendarpanel" style="position: absolute;display: none;z-index:9999; background-color: #ffffff;border: 1px solid #cccccc;width:175px;font-size:12px;"></div>';
    if(document.all)
    {
        str += '<iframe style="position:absolute;z-index:2000;width:expression(this.previoussibling.offsetwidth);';
        str += 'height:expression(this.previoussibling.offsetheight);';
        str += 'left:expression(this.previoussibling.offsetleft);top:expression(this.previoussibling.offsettop);';
        str += 'display:expression(this.previoussibling.style.display);" scrolling="no" frameborder="no"></iframe>';
    }
    var div = document.createelement("div");
    div.innerhtml = str;
    div.id = "containerpanel";
    div.style.display ="none";
    document.body.appendchild(div);
}//调用calendar.show(datecontrol, popcontrol);
//-->

将上述代码另存为 webcalendar.js,页面调用代码
复制代码 代码如下:

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="head1">
        <script src="webcalendar.js" type="text/javascript"></script>
</head>
<body>
<form name="form1" method="post" action="webform1.aspx" id="form1">
<input type="text" value="" maxlength="100" id="txt_createdatest01" onclick="selectdate(this,'yyyy 年')" readonly="true" style="width:265px;cursor:pointer" /><br />
<input type="text" value="" maxlength="100" id="txt_createdatest02" onclick="selectdate(this,'yyyy 年 mm 月')" readonly="true" style="width:265px;cursor:pointer" /><br />
<input type="text" value="" maxlength="100" id="txt_createdatest03" onclick="selectdate(this,'yyyy\/mm\/dd')" readonly="true" style="width:265px;cursor:pointer" /><br />
<input type="text" value="" maxlength="100" id="text1" onclick="selectdate(this,'yyyy-mm-dd hh时')" readonly="true" style="width:265px;cursor:pointer" /><br />
<input type="text" value="" maxlength="100" id="text3" onclick="selectdate(this,'yyyy-mm-dd hh:mm')" readonly="true" style="width:265px;cursor:pointer" /><br />
<input type="text" value="" maxlength="100" id="text4" onclick="selectdate(this,'yyyy年mm月dd日 hh时mm分ss秒',0,-150)" readonly="true" style="width:265px;cursor:pointer" /><br />
</form>
</body>
</html>

调用方法:
1、传入对象:selectdate(this,'yyyy 年')
2、传入 id:selectdatebyid('txt_createdatest01','yyyy 年')
3、参数 selectdate(this,'yyyy 年',0,-150)
格式(注意大小写):yyyy→年,mm→月,dd→天,hh→24小时制,mm→分钟,ss→秒
0 → 相对于文本框的横向偏移量
-150 → 相对于文本框的纵向偏移量