首先通过windos窗口加载
window.onload = function () {
}
获取当前时间
//获取动态日期
var new_time = new Date();
创建div便于定位丶好看
//在body中创建box
var box = document.createElement("div");
box.className = "box";
document.body.appendChild(box);
创建下来选择框select(选择框)和option(下拉框)
//创建年月日的select 和option
var year_select = document.createElement("select");
year_select.id = "year";
box.appendChild(year_select);
box.innerHTML += "年";
for (i=year;i>parseInt(year)-60;i--){
document.getElementById("year").add(new Option(i,i));
}// 年 完成
var month_select = document.createElement("select");
month_select.id = "month";
box.appendChild(month_select);
box.innerHTML += "月";
for (i=1;i<=12;i++){
document.getElementById("month").add(new Option(i,i));
}// 月 完成
var day_select = document.createElement("select");
day_select.id = "day";
box.appendChild(day_select);
box.innerHTML += "日";
for (i=1;i<=31;i++){
document.getElementById("day").add(new Option(i,i));
}// 日 完成
完整版js
//窗口加载时生成内容
window.onload = function () {
//获取动态日期
var new_time = new Date();
var year = new_time.getFullYear();
var month = new_time.getMonth();
var day = new_time.getDay();
//在body中创建box
var box = document.createElement("div");
box.className = "box";
document.body.appendChild(box);
//创建年月日的select 和option
var year_select = document.createElement("select");
year_select.id = "year";
box.appendChild(year_select);
box.innerHTML += "年";
for (i=year;i>parseInt(year)-60;i--){
document.getElementById("year").add(new Option(i,i));
}// 年 完成
var month_select = document.createElement("select");
month_select.id = "month";
box.appendChild(month_select);
box.innerHTML += "月";
for (i=1;i<=12;i++){
document.getElementById("month").add(new Option(i,i));
}// 月 完成
var day_select = document.createElement("select");
day_select.id = "day";
box.appendChild(day_select);
box.innerHTML += "日";
for (i=1;i<=31;i++){
document.getElementById("day").add(new Option(i,i));
}// 日 完成
}
最后引用到HTML代码的head就ok了