//JavaScript
function Calendar()
{
//如果输入三个参数则将这三个参数作为年月日对象属性
if(arguments.length==3)
{
this.year = arguments[0];
this.month = arguments[1];
this.date = arguments[2];
}
else//如果无参数则使用系统年月日作为对象属性
{
var dt = new Date();
this.year = dt.getYear();
this.month = dt.getMonth()+1;
this.date = dt.getDate();
} //判断最大天数
this.getMaxDays = function()
{
if(this.month < 7)
(this.month%2==0) ? this.maxDays = 30:this.maxDays = 31;
else
(this.month%2==0) ? maxDays = 31:maxDays = 32;
if(this.month == 2)
((this.year%4==0)&&(this.year%100==0)||(this.year%400==0))? this.maxDays = 29:this.maxDays = 28;
}
//重载toString函数
this.toString=function()
{
return this.year+"-"+this.month+"-"+this.date+"-"+this.maxDays;
}; this.divCal = null;
//用以显示日历框界面
this.Show = function(cx,cy)
{
//在界面下显示一个阴影
var shadow = document.createElement("div");
with(shadow.style)
{
position = "absolute";
left = cx+5;
top = cy+5;
width = "200px";
height= "180px";
textAlign="center";
fontSize="14px";
background="#666666";
filter="Alpha('Opactiy=50')";
}
document.body.appendChild(shadow);
//添加日历DIV
this.divCal = document.createElement("div");
with(this.divCal.style)
{
position = "absolute";
left = cx;
top = cy;
width = "200px";
height= "180px";
border ="1px solid #888888";
textAlign="center";
fontSize="14px";
background="#ffffff";
}
document.body.appendChild(this.divCal);
//添加一个SELECT控件显示年份
var seleYear = document.createElement("select");
for(var y=1980;y<2030;y++)
seleYear.options.add(new Option(y,y));
seleYear.value = this.year;
this.divCal.appendChild(seleYear);
seleYear.onchange = function()
{
this.year = seleYear.value;
this.Update();//运行后改变控件值时这行报错:对象不支持此属性或方法 }
//添加一个SELECT控件显示月份
var seleMonth = document.createElement("select");
for(var m=1;m<=12;m++)
seleMonth.options.add(new Option(m,m));
seleMonth.value = this.month;
this.divCal.appendChild(seleMonth);
//以表格形式显示日期
var tabCal = document.createElement("table");
tabCal.style.width="100%";
tabCal.style.textAlign="center";
for(var i=0;i<7;i++)
{
tabCal.insertRow(i);
for(var j=0;j<7;j++)
{
tabCal.rows[i].insertCell(j);
}
}
tabCal.rows[0].cells[0].innerHTML="日";
tabCal.rows[0].cells[1].innerHTML="一";
tabCal.rows[0].cells[2].innerHTML="二";
tabCal.rows[0].cells[3].innerHTML="三";
tabCal.rows[0].cells[4].innerHTML="四";
tabCal.rows[0].cells[5].innerHTML="五";
tabCal.rows[0].cells[6].innerHTML="六";
this.divCal.appendChild(tabCal);
this.Update();
}
//用以更新日期显示
this.Update=function()
{
var tabCal = this.divCal.getElementsByTagName("table")[0];
var num=1;
var dt=new Date();
dt.setYear(this.year);
dt.setMonth(this.month-1);
dt.setDate(1);
var offset = parseInt(dt.getDay());
this.getMaxDays();
for(var i=1;i<7;i++)
{
for(var j=0;j<7;j++)
{
var day = (num++)-offset;
if(day>0&&day<=this.maxDays)
tabCal.rows[i].cells[j].innerHTML=day;
}
}
};
}var objCal = new Calendar();
function Calendar()
{
//如果输入三个参数则将这三个参数作为年月日对象属性
if(arguments.length==3)
{
this.year = arguments[0];
this.month = arguments[1];
this.date = arguments[2];
}
else//如果无参数则使用系统年月日作为对象属性
{
var dt = new Date();
this.year = dt.getYear();
this.month = dt.getMonth()+1;
this.date = dt.getDate();
} //判断最大天数
this.getMaxDays = function()
{
if(this.month < 7)
(this.month%2==0) ? this.maxDays = 30:this.maxDays = 31;
else
(this.month%2==0) ? maxDays = 31:maxDays = 32;
if(this.month == 2)
((this.year%4==0)&&(this.year%100==0)||(this.year%400==0))? this.maxDays = 29:this.maxDays = 28;
}
//重载toString函数
this.toString=function()
{
return this.year+"-"+this.month+"-"+this.date+"-"+this.maxDays;
}; this.divCal = null;
//用以显示日历框界面
this.Show = function(cx,cy)
{
//在界面下显示一个阴影
var shadow = document.createElement("div");
with(shadow.style)
{
position = "absolute";
left = cx+5;
top = cy+5;
width = "200px";
height= "180px";
textAlign="center";
fontSize="14px";
background="#666666";
filter="Alpha('Opactiy=50')";
}
document.body.appendChild(shadow);
//添加日历DIV
this.divCal = document.createElement("div");
with(this.divCal.style)
{
position = "absolute";
left = cx;
top = cy;
width = "200px";
height= "180px";
border ="1px solid #888888";
textAlign="center";
fontSize="14px";
background="#ffffff";
}
document.body.appendChild(this.divCal);
//添加一个SELECT控件显示年份
var seleYear = document.createElement("select");
for(var y=1980;y<2030;y++)
seleYear.options.add(new Option(y,y));
seleYear.value = this.year;
this.divCal.appendChild(seleYear);
seleYear.onchange = function()
{
this.year = seleYear.value;
this.Update();//运行后改变控件值时这行报错:对象不支持此属性或方法 }
//添加一个SELECT控件显示月份
var seleMonth = document.createElement("select");
for(var m=1;m<=12;m++)
seleMonth.options.add(new Option(m,m));
seleMonth.value = this.month;
this.divCal.appendChild(seleMonth);
//以表格形式显示日期
var tabCal = document.createElement("table");
tabCal.style.width="100%";
tabCal.style.textAlign="center";
for(var i=0;i<7;i++)
{
tabCal.insertRow(i);
for(var j=0;j<7;j++)
{
tabCal.rows[i].insertCell(j);
}
}
tabCal.rows[0].cells[0].innerHTML="日";
tabCal.rows[0].cells[1].innerHTML="一";
tabCal.rows[0].cells[2].innerHTML="二";
tabCal.rows[0].cells[3].innerHTML="三";
tabCal.rows[0].cells[4].innerHTML="四";
tabCal.rows[0].cells[5].innerHTML="五";
tabCal.rows[0].cells[6].innerHTML="六";
this.divCal.appendChild(tabCal);
this.Update();
}
//用以更新日期显示
this.Update=function()
{
var tabCal = this.divCal.getElementsByTagName("table")[0];
var num=1;
var dt=new Date();
dt.setYear(this.year);
dt.setMonth(this.month-1);
dt.setDate(1);
var offset = parseInt(dt.getDay());
this.getMaxDays();
for(var i=1;i<7;i++)
{
for(var j=0;j<7;j++)
{
var day = (num++)-offset;
if(day>0&&day<=this.maxDays)
tabCal.rows[i].cells[j].innerHTML=day;
}
}
};
}var objCal = new Calendar();
解决方案 »
- javascript怎么分段上传文件?
- 如何判断日期输入
- 有关JS for in的特殊写法
- firefox中实现ie的showModalDialog效果,但modal=yes不管用。
- 我想使IE最大化(没有地址等),不用OPEN,在本页面上怎么实现了,大侠请进!!!
- 下拉框的选择问题
- 请问怎样用javascript阻止页面刷新?
- 打开窗体的一个问题(100相求)
- 如何得到点击元属的id和index?
- 求助高手帮忙,下拉到底部会让尾部的div显示,然后继续下拉尾部又消失了,怎么才能下拉到尾部时,尾部内容能出现,然后滚动条到下面,然后上拉内容消失?
- javascript 中使用AJAX乱码问题?
- 仿google眼睛
{
this.year = seleYear.value;
this.Update();//运行后改变控件值时这行报错:对象不支持此属性或方法
} 这个函数里面应该this指向的是seleYear了吧
seleYear.onchange = function(this) 这样试试
在seleYear.onchange中.this指的是seleYear,而不是Calendar对象
<html>
<body>
<script type = "text/javascript">
//JavaScript
function Calendar() {
//如果输入三个参数则将这三个参数作为年月日对象属性
if (arguments.length == 3) {
this.year = arguments[0];
this.month = arguments[1];
this.date = arguments[2];
}
else //如果无参数则使用系统年月日作为对象属性
{
var dt = new Date();
this.year = dt.getYear();
this.month = dt.getMonth() + 1;
this.date = dt.getDate();
} //判断最大天数
this.getMaxDays = function() {
if (this.month < 7)(this.month % 2 == 0) ? this.maxDays = 30 : this.maxDays = 31;
else(this.month % 2 == 0) ? maxDays = 31 : maxDays = 32;
if (this.month == 2)((this.year % 4 == 0) && (this.year % 100 == 0) || (this.year % 400 == 0)) ? this.maxDays = 29 : this.maxDays = 28;
};
//重载toString函数
this.toString = function() {
return this.year + "-" + this.month + "-" + this.date + "-" + this.maxDays;
}; this.divCal = null;
//用以显示日历框界面
this.Show = function(cx, cy) {
//在界面下显示一个阴影
var shadow = document.createElement("div");
with(shadow.style) {
position = "absolute";
left = cx + 5;
top = cy + 5;
width = "200px";
height = "180px";
textAlign = "center";
fontSize = "14px";
background = "#666666";
filter = "Alpha('Opactiy=50')";
}
document.body.appendChild(shadow);
//添加日历DIV
this.divCal = document.createElement("div");
with(this.divCal.style) {
position = "absolute";
left = cx;
top = cy;
width = "200px";
height = "180px";
border = "1px solid #888888";
textAlign = "center";
fontSize = "14px";
background = "#ffffff";
}
document.body.appendChild(this.divCal);
//添加一个SELECT控件显示年份
var seleYear = document.createElement("select");
for (var y = 1980; y < 2030; y++)
seleYear.options.add(new Option(y, y));
seleYear.value = this.year;
this.divCal.appendChild(seleYear);
var self = this;
seleYear.onchange = function() {
self.year = this.value;
self.Update(); //运行后改变控件值时这行报错:对象不支持此属性或方法 }
}
//添加一个SELECT控件显示月份
var seleMonth = document.createElement("select");
for (var m = 1; m <= 12; m++)
seleMonth.options.add(new Option(m, m));
seleMonth.value = this.month;
this.divCal.appendChild(seleMonth);
seleMonth.onchange = function(){
self.month = this.value;
self.Update();
};
//以表格形式显示日期
var tabCal = document.createElement("table");
tabCal.style.width = "100%";
tabCal.style.textAlign = "center";
for (var i = 0; i < 7; i++) {
tabCal.insertRow(i);
for (var j = 0; j < 7; j++) {
tabCal.rows[i].insertCell(j);
}
}
tabCal.rows[0].cells[0].innerHTML = "日";
tabCal.rows[0].cells[1].innerHTML = "一";
tabCal.rows[0].cells[2].innerHTML = "二";
tabCal.rows[0].cells[3].innerHTML = "三";
tabCal.rows[0].cells[4].innerHTML = "四";
tabCal.rows[0].cells[5].innerHTML = "五";
tabCal.rows[0].cells[6].innerHTML = "六"; this.divCal.appendChild(tabCal);
this.Update();
}
//用以更新日期显示
this.Update = function() {
var tabCal = this.divCal.getElementsByTagName("table")[0];
var num = 1;
var dt = new Date();
dt.setYear(this.year);
dt.setMonth(this.month - 1);
dt.setDate(1);
var offset = parseInt(dt.getDay());
this.getMaxDays();
for (var i = 1; i < 7; i++) {
for (var j = 0; j < 7; j++) {
var day = (num++) - offset;
if (day > 0 && day <= this.maxDays)
tabCal.rows[i].cells[j].innerHTML = day;
else
tabCal.rows[i].cells[j].innerHTML = "";
}
}
};
}
var objCal = new Calendar();
objCal.Show(100, 100);
</script>
</body>
</html>