您好,欢迎访问一九零五行业门户网

一个日期下拉菜单的js实现代码_时间日期

1.先看效果图:
2.js代码
year_month_day.js
复制代码 代码如下:
year_month_day.js
function dateselector(selyear, selmonth, selday) {
this.selyear = selyear;
this.selmonth = selmonth;
this.selday = selday;
this.selyear.group = this;
this.selmonth.group = this;
// 给年份、月份下拉菜单添加处理onchange事件的函数
if (window.document.all != null) // ie
{
this.selyear.attachevent(onchange, dateselector.onchange);
this.selmonth.attachevent(onchange, dateselector.onchange);
}
else // firefox
{
this.selyear.addeventlistener(change, dateselector.onchange, false);
this.selmonth.addeventlistener(change, dateselector.onchange, false);
}
if (arguments.length == 4) // 如果传入参数个数为4,最后一个参数必须为date对象
this.initselector(arguments[3].getfullyear(), arguments[3].getmonth() + 1, arguments[3].getdate());
else if (arguments.length == 6) // 如果传入参数个数为6,最后三个参数必须为初始的年月日数值
this.initselector(arguments[3], arguments[4], arguments[5]);
else // 默认使用当前日期
{
var dt = new date();
this.initselector(dt.getfullyear(), dt.getmonth() + 1, dt.getdate());
}
}
// 增加一个最大年份的属性
dateselector.prototype.minyear = 1900;
// 增加一个最大年份的属性
dateselector.prototype.maxyear = (new date()).getfullyear();
// 初始化年份
dateselector.prototype.inityearselect = function () {
// 循环添加opion元素到年份select对象中
for (var i = this.maxyear; i >= this.minyear; i--) {
// 新建一个option对象
var op = window.document.createelement(option);
// 设置option对象的值
op.value = i;
// 设置option对象的内容
op.innerhtml = i;
// 添加到年份select对象
this.selyear.appendchild(op);
}
}
// 初始化月份
dateselector.prototype.initmonthselect = function () {
// 循环添加opion元素到月份select对象中
for (var i = 1; i // 新建一个option对象
var op = window.document.createelement(option);
// 设置option对象的值
op.value = i;
// 设置option对象的内容
op.innerhtml = i;
// 添加到月份select对象
this.selmonth.appendchild(op);
}
}
// 根据年份与月份获取当月的天数
dateselector.daysinmonth = function (year, month) {
var date = new date(year, month, 0);
return date.getdate();
}
// 初始化天数
dateselector.prototype.initdayselect = function () {
// 使用parseint函数获取当前的年份和月份
var year = parseint(this.selyear.value);
var month = parseint(this.selmonth.value);
// 获取当月的天数
var daysinmonth = dateselector.daysinmonth(year, month);
// 清空原有的选项
this.selday.options.length = 0;
// 循环添加opion元素到天数select对象中
for (var i = 1; i // 新建一个option对象
var op = window.document.createelement(option);
// 设置option对象的值
op.value = i;
// 设置option对象的内容
op.innerhtml = i;
// 添加到天数select对象
this.selday.appendchild(op);
}
}
// 处理年份和月份onchange事件的方法,它获取事件来源对象(即selyear或selmonth)
// 并调用它的group对象(即dateselector实例,请见构造函数)提供的initdayselect方法重新初始化天数
// 参数e为event对象
dateselector.onchange = function (e) {
var selector = window.document.all != null ? e.srcelement : e.target;
selector.group.initdayselect();
}
// 根据参数初始化下拉菜单选项
dateselector.prototype.initselector = function (year, month, day) {
// 由于外部是可以调用这个方法,因此我们在这里也要将selyear和selmonth的选项清空掉
// 另外因为initdayselect方法已经有清空天数下拉菜单,因此这里就不用重复工作了
this.selyear.options.length = 0;
this.selmonth.options.length = 0;
// 初始化年、月
this.inityearselect();
this.initmonthselect();
// 设置年、月初始值
this.selyear.selectedindex = this.maxyear - year;
this.selmonth.selectedindex = month - 1;
// 初始化天数
this.initdayselect();
// 设置天数初始值
this.selday.selectedindex = day - 1;
}
3.html代码
year_month_day.htm
复制代码 代码如下:
year_month_day.htm
濡
其它类似信息

推荐信息