asfman
android developer
posts - 90,  comments - 213,  trackbacks - 0

<script language=Javascript>
function objFocus(n) //控制光标选中光标后一字符
{
    if (arguments.length == 0) var n=0;
    var e = window.event.srcElement;
    var r = e.createTextRange();
    r.moveStart('character', n);
    r.collapse(true);
    r.select();
    var s = document.selection.createRange().duplicate().duplicate();
    s.moveStart('character', 1);
    s.setEndPoint("EndToEnd", r);
    s.select();
}
function keyPress(e) //手工输入日期
{
    var k       = String.fromCharCode(window.event.keyCode);
    if(!/^\d/.test(k)) return false; //不准输入非法字符

    e.focus();
    var range   = e.createTextRange();
    var select1 = document.selection.createRange().duplicate();
    select1.setEndPoint("StartToStart",range);

    var s1      = select1.text;                 //得到光标左边的字串
    var s2      = e.value.substr(s1.length);    //得到光标右边的字串
    e.value     = s1.substring(0, s1.length-1) + k + s2; //给文本框赋值

    var n       = s1.length;
    if (n==4 || n==7 || n==10 || n==13) n++;
    objFocus(n);

    window.event.keyCode = 0;
    event.returnValue = false;
}
function keyDown(e)  //删除回退按方向键时的处理
{
    var k   = window.event.keyCode;
    if (!(k>=48 && k<=57    //数字0-9
        || k==46            //删除键 Del
        || k==8             //回删链 Backspace
        || k==37            //方向链 ←
        || k==39            //方向键 →
        || k==9             //制表键 Tab
        || k==13            //回车键 Enter
        )) return false;    //屏蔽非上面所列的键
        e.focus();

    var range   = e.createTextRange();
    var select1 = document.selection.createRange().duplicate();
    select1.setEndPoint("StartToStart",range);

    var s1      = select1.text;                 //得到光标左边的字串
    var s2      = e.value.substr(s1.length);    //得到光标右边的字串
    var n       = s1.length;

    switch(k)
    {
        case 8 :
            e.value = s1.substring(0, n-1) + "0" + s2;
            if (n==6 || n==9 || n==12 || n==15) n--;
            objFocus(n-2);
            window.event.keyCode = 0;
            event.returnValue = false;
            break;
        case 46 :
            e.value = s1.substring(0, n-1) + "0" + s2;
            if (n==4 || n==7 || n==10 || n==13) n++;
            objFocus(n);
            window.event.keyCode = 0;
            event.returnValue = false;
            break;
        case 37 :
            if (n==6 || n==9 || n==12 || n==15) n--;
            if (n == e.value.length)
            {
                if (document.selection.createRange().text == "")
                    objFocus(n-1);
                else
                 objFocus(n - 2);
            }
            else
                objFocus(n - 2);
            window.event.keyCode = 0;
            event.returnValue = false;
            break;
        case 39 :
            if (n==4 || n==7 || n==10 || n==13) n++;
            objFocus(n);
            window.event.keyCode = 0;
            event.returnValue = false;
            break;
    }
}
</script>

<form name=form1>
<input
    name        = "txt"
    maxlength   = "16"
    size        = "18"
    value       = "0000-00-00 00:00"
    onfocus     = "objFocus()"
    onkeydown   = "return keyDown(this)"
    onkeypress  = "return keyPress(this)"
    onpaste     = "return false"
    ondragenter = "return false"
    style       = "ime-mode:Disabled"
>
</form>

posted on 2006-04-03 15:43 汪杰 阅读(269) 评论(0)  编辑 收藏 引用 所属分类: div css dhtml
只有注册用户登录后才能发表评论。

<2024年11月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(15)

随笔分类(1)

随笔档案(90)

文章分类(727)

文章档案(712)

相册

收藏夹

http://blog.csdn.net/prodigynonsense

友情链接

最新随笔

搜索

  •  

积分与排名

  • 积分 - 467490
  • 排名 - 6

最新随笔

最新评论

阅读排行榜

评论排行榜