不错的一个日期输入 动态
不错的一个日期输入 动态
发布时间:2016-12-30 来源:查字典编辑
摘要:复制代码代码如下:日期控件varagt=navigator.userAgent.toLowerCase();varOldTextBox;fu...

复制代码 代码如下:

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<head>

<metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/>

<title>日期控件</title>

<script>

varagt=navigator.userAgent.toLowerCase();

varOldTextBox;

functionDateSeter(obj){

varparent=obj.parentNode;

varselYear=document.createElement('SELECT');

selYear.id='LoftyYear';

parent.insertBefore(selYear,obj);

varselMonth=document.createElement('SELECT');

selMonth.id='LoftyMonth';

parent.insertBefore(selMonth,obj);

varselDay=document.createElement('SELECT');

selDay.id='LoftyDay';

parent.insertBefore(selDay,obj);

varbtnSubmit=document.createElement('BUTTON');

btnSubmit.style.width='22px';

parent.insertBefore(btnSubmit,obj);

btnSubmit.id='LoftyBtn';

for(sinbtnSubmit)

{

//document.write(s+'<br/>');

}

btnSubmit.textContent='OK';

btnSubmit.value='OK';

al(btnSubmit,'click','Restore()');

OldTextBox=obj;

parent.removeChild(obj);

varnow=newDate();

varcYear=now.getFullYear();

SetCurrentYear(selYear,cYear);

al(selYear,'change','SetCurrentYear(document.getElementById('LoftyYear'))');

al(selMonth,'change','SetDay(null,null)');

for(i=1;i<=12;i++)

{

varop=newOption(i,i);

selMonth.options.add(op);

}

varcMonth=now.getMonth()+1;

SetDay(cYear,cMonth);

selDay.selectedIndex=now.getDate()-1;

}

functionRestore()

{

varyy=document.getElementById('LoftyYear');

varmm=document.getElementById('LoftyMonth');

vard=document.getElementById('LoftyDay');

varbtn=document.getElementById('LoftyBtn');

vartime=yy.options[yy.selectedIndex].value+'-'+mm.options[mm.selectedIndex].value+'-'+d.options[d.selectedIndex].value;

OldTextBox.value=time;

varparent=yy.parentNode;

parent.insertBefore(OldTextBox,yy);

parent.removeChild(yy);

parent.removeChild(mm);

parent.removeChild(d);

parent.removeChild(btn);

}

functionSetDay(year,month)

{

if(year==null){

varyy=document.getElementById('LoftyYear');

varmm=document.getElementById('LoftyMonth');

year=Math.round(yy.options[yy.selectedIndex].value);

month=Math.round(mm.options[mm.selectedIndex].value);

}

vardate=newDate(year,month,1);

date.setDate(0);

varmaxdate=date.getDate();

varobj=document.getElementById('LoftyDay');

varseldate=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);

}

functionSetCurrentYear(selecter,currYear)

{

if(currYear==null){

currYear=Math.round(selecter.options[selecter.selectedIndex].value);

}

reAppendSelect(selecter,currYear-10,currYear+10,currYear);

}

functionreAppendSelect(selecter,startIndex,endIndex,selIndex)

{

while(selecter.options.length>0)

{

selecter.removeChild(selecter.options[selecter.options.length-1]);

}

for(i=startIndex;i<=endIndex;i++)

{

varop=newOption(i,i);

selecter.options.add(op);

op.selected=(selIndex==i);

}

}

functional(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>

<inputonclick="DateSeter(this);"/>

</body>

</html>

精简只有年:

复制代码 代码如下:

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<head>

<metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/>

<title>日期控件</title>

<script>

varagt=navigator.userAgent.toLowerCase();

varOldTextBox;

functionDateSeter(obj){

varparent=obj.parentNode;

varselYear=document.createElement('SELECT');

selYear.id='LoftyYear';

parent.insertBefore(selYear,obj);

/*

varbtnSubmit=document.createElement('BUTTON');

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

vardefaultValue=obj.value;

varnow=newDate();

varcYear=now.getFullYear();

SetCurrentYear(selYear,cYear,defaultValue);

al(selYear,'change','Restore(obj)');

}

functionRestore()

{

OldTextBox.value=arguments[0].value;

varyy=document.getElementById('LoftyYear');

varparent=yy.parentNode;

parent.insertBefore(OldTextBox,yy);

parent.removeChild(yy);

/*

varyy=document.getElementById('LoftyYear');

varbtn=document.getElementById('LoftyBtn');

vartime=yy.options[yy.selectedIndex].value;

OldTextBox.value=time;

varparent=yy.parentNode;

parent.insertBefore(OldTextBox,yy);

parent.removeChild(yy);

parent.removeChild(btn);

*/

}

functionSetCurrentYear(selecter,currYear)

{

if(currYear==null){

currYear=Math.round(selecter.options[selecter.selectedIndex].value);

}

vardv=arguments[2];

reAppendSelect(selecter,1901,2001,currYear,dv);

}

functionreAppendSelect(selecter,startIndex,endIndex,selIndex)

{

while(selecter.options.length>0)

{

selecter.removeChild(selecter.options[selecter.options.length-1]);

}

for(i=startIndex;i<=endIndex;i++)

{

varop=newOption(i,i);

selecter.options.add(op);

if(arguments[4]!=""){

if(i==arguments[4])

op.selected=true;

}else{

if(i==endIndex)

op.selected=true;

}

}

}

functional(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>

<inputonclick="DateSeter(this);"/>

</body>

</html>

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新Javascript教程学习
热门Javascript教程学习
编程开发子分类