//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();

解决方案 »

  1.   

    seleYear.onchange = function()
    {
    this.year = seleYear.value;
    this.Update();//运行后改变控件值时这行报错:对象不支持此属性或方法 
    } 这个函数里面应该this指向的是seleYear了吧
      

  2.   

    那应该怎么修改?我把this改为Calendar还是报同样的错误信息啊
      

  3.   

    seleYear.onchange = function() 这个函数参数传个THIS进去试试看,应该可以覆盖掉函数内部的this,不保证能成功...
      

  4.   

    不好意思,昨天忙,就忘记上了
    seleYear.onchange = function(this) 这样试试
      

  5.   

    改了一下,可以显示了,
    在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>
      

  6.   

    8楼的做法不知道传THIS行不行?能不能覆盖掉函数内部的THIS呢?