不错的一个日期输入 动态

2016-02-19 11:42 3 1 收藏

下面图老师小编跟大家分享一个简单易学的不错的一个日期输入 动态教程,get新技能是需要行动的,喜欢的朋友赶紧收藏起来学习下吧!

【 tulaoshi.com - Web开发 】

代码如下:

!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日期控件/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('BUTTON');
    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(selYear,'change','SetCurrentYear(document.getElementById('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.length0){
        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.length0)
    {
        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


精简只有年:
代码如下:

!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日期控件/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('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);
    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,1901,2001,currYear,dv);
}
function reAppendSelect(selecter,startIndex,endIndex,selIndex)
{
    while(selecter.options.length0)
    {
        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

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

来源:https://www.tulaoshi.com/n/20160219/1598306.html

延伸阅读
社区问的人太多了,保存一个备用 --建立测试环境 set nocount on create table test(model varchar(20),date int ,qty int) insert into test select 'a','8','10' insert into test select 'a','10','50' insert into test select 'b','8','100' insert i...
标签: 办公软件
当编辑科技文档时,常常需要输入分数,但Word不能直接输入分数。有两种实现方法:用 Word本身提供的的EQ(方程)域或用公式编辑器。这两种方法都不简便,笔者编写了一个EnterFraction宏命令可以在文档中方便地插入分数,大大简化了分数的编辑工作。下面详细介绍如何在Word 97/2000/XP中创建该宏命令。 1.创建一个对话窗口 单击“工...
标签: Web开发
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行] 更多代码下载:http://www.jb51.net/jiaoben/list500_1.html
var posYoubiao:Array = new Array();   posYoubiao[0] = 5;   for (i=1; i<6; i++) {           //预先设置好游标的位置           posYoubiao[i] ...

经验教程

41

收藏

87
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部