Copy codeThe Code is as follows: <! 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>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> date Control </title>
<Script>
Var agt = navigator. userAgent. toLowerCase ();
Var OldTextBox;
Function DateSeter (obj ){
Var parent = obj. parentNode;
Var selYear = document. createElement ('select ');
SelYear. id = 'loftyyear ';
Parent. insertBefore (selYear, obj );
Var selMonth = document. createElement ('select ');
SelMonth. id = 'loftymonth ';
Parent. insertBefore (selMonth, obj );
Var selDay = document. createElement ('select ');
SelDay. id = 'loftyday ';
Parent. insertBefore (selDay, obj );
Var btnSubmit = document. createElement ('click ');
BtnSubmit. style. width = '22px ';
Parent. insertBefore (btnSubmit, obj );
BtnSubmit. id = 'loftybtn ';
For (s in btnSubmit)
{
// Document. write (s + '<br/> ');
}
BtnSubmit. textContent = 'OK ';
BtnSubmit. value = 'OK ';
Al (btnSubmit, 'click', 'Restore ()');
OldTextBox = obj;
Parent. removeChild (obj );
Var now = new Date ();
Var cYear = now. getFullYear ();
SetCurrentYear (selYear, cYear );
Al (selelementbyid (\ 'loftyyear \'))');
Al (selMonth, 'change', 'setday (null, null )');
For (I = 1; I <= 12; I ++)
{
Var op = new Option (I, I );
SelMonth. options. add (op );
}
Var cMonth = now. getMonth () + 1;
SetDay (cYear, cMonth );
SelDay. selectedIndex = now. getDate ()-1;
}
Function Restore ()
{
Var yy = document. getElementById ('loftyyear ');
Var mm = document. getElementById ('loftymonth ');
Var d = document. getElementById ('loftyday ');
Var btn = document. getElementById ('loftybtn ');
Var time = yy. options [yy. selectedIndex]. value + '-' + mm. options [mm. selectedIndex]. value + '-' + d. options [d. selectedIndex]. value;
OldTextBox. value = time;
Var parent = yy. parentNode;
Parent. insertBefore (OldTextBox, yy );
Parent. removeChild (yy );
Parent. removeChild (mm );
Parent. removeChild (d );
Parent. removeChild (btn );
}
Function SetDay (year, month)
{
If (year = null ){
Var yy = document. getElementById ('loftyyear ');
Var mm = document. getElementById ('loftymonth ');
Year = Math. round (yy. options [yy. selectedIndex]. value );
Month = Math. round (mm. options [mm. selectedIndex]. value );
}
Var date = new Date (year, month, 1 );
Date. setDate (0 );
Var maxdate = date. getDate ();
Var obj = document. getElementById ('loftyday ');
Var seldate = obj. selectedIndex;
If (obj. options. length> 0 ){
If (Math. round (obj. options [obj. selectedIndex]. value)> maxdate)
{
Seldate = maxdate-1;
}
}
ReAppendSelect (obj, 1, maxdate, seldate + 1 );
}
Function SetCurrentYear (selecter, currYear)
{
If (currYear = null ){
CurrYear = Math. round (selecter. options [selecter. selectedIndex]. value );
}
ReAppendSelect (selecter, currYear-10, currYear + 10, currYear );
}
Function reAppendSelect (selecter, startIndex, endIndex, selIndex)
{
While (selecter. options. length> 0)
{
Selecter. removeChild (selecter. options [selecter. options. length-1]);
}
For (I = startIndex; I <= endIndex; I ++)
{
Var op = new Option (I, I );
Selecter. options. add (op );
Op. selected = (selIndex = I );
}
}
Function al (obj, lissener, func ){
If (agt. indexOf ('Firefox ')! =-1 ){
Obj. addEventListener (lissener, function () {eval (func) ;}, false );
} Else {
Obj. attachEvent ('on' + lissener, function () {eval (func );});
}
}
</Script>
</Head>
<Body>
<Input onclick = "DateSeter (this);"/>
</Body>
</Html>
Simplified for only one year:
Copy codeThe Code is as follows: <! 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>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> date Control </title>
<Script>
Var agt = navigator. userAgent. toLowerCase ();
Var OldTextBox;
Function DateSeter (obj ){
Var parent = obj. parentNode;
Var selYear = document. createElement ('select ');
SelYear. id = 'loftyyear ';
Parent. insertBefore (selYear, obj );
/*
Var btnSubmit = document. createElement ('click ');
BtnSubmit. style. width = '32px ';
Parent. insertBefore (btnSubmit, obj );
BtnSubmit. id = 'loftybtn ';
BtnSubmit. textContent = 'OK ';
BtnSubmit. value = 'OK ';
Al (btnSubmit, 'click', 'Restore ()');
*/
OldTextBox = obj;
Parent. removeChild (obj );
Var defaultValue = obj. value;
Var now = new Date ();
Var cYear = now. getFullYear ();
SetCurrentYear (selYear, cYear, defaultValue );
Al (selYear, 'change', 'Restore (obj )');
}
Function Restore ()
{
OldTextBox. value = arguments [0]. value;
Var yy = document. getElementById ('loftyyear ');
Var parent = yy. parentNode;
Parent. insertBefore (OldTextBox, yy );
Parent. removeChild (yy );
/*
Var yy = document. getElementById ('loftyyear ');
Var btn = document. getElementById ('loftybtn ');
Var time = yy. options [yy. selectedIndex]. value;
OldTextBox. value = time;
Var parent = yy. parentNode;
Parent. insertBefore (OldTextBox, yy );
Parent. removeChild (yy );
Parent. removeChild (btn );
*/
}
Function SetCurrentYear (selecter, currYear)
{
If (currYear = null ){
CurrYear = Math. round (selecter. options [selecter. selectedIndex]. value );
}
Var dv = arguments [2];
ReAppendSelect (selecter, iterator, currYear, dv );
}
Function reAppendSelect (selecter, startIndex, endIndex, selIndex)
{
While (selecter. options. length> 0)
{
Selecter. removeChild (selecter. options [selecter. options. length-1]);
}
For (I = startIndex; I <= endIndex; I ++)
{
Var op = new Option (I, I );
Selecter. options. add (op );
If (arguments [4]! = ""){
If (I = arguments [4])
Op. selected = true;
} Else {
If (I = endIndex)
Op. selected = true;
}
}
}
Function al (obj, lissener, func ){
If (agt. indexOf ('Firefox ')! =-1 ){
Obj. addEventListener (lissener, function () {eval (func) ;}, false );
} Else {
Obj. attachEvent ('on' + lissener, function () {eval (func );});
}
}
</Script>
</Head>
<Body>
<Input onclick = "DateSeter (this);"/>
</Body>
</Html>