表单中按上下方向键自动移动光标的例子

发表于:2007-06-30来源:作者:点击数: 标签:
html head script language= java script function go1() { key=window.event.keyCode; if (key==38){ CurTabIndex=event.srcElement.tabIndex-1 for (n=0;nform1.elements.length;n++){ if (form1.elements[n].tabIndex==CurTabIndex){ form1.elements[n].fo
<html>
<head>
<script language="javascript">
function go1()
{
key=window.event.keyCode;
if (key==38){
    CurTabIndex=event.srcElement.tabIndex-1
    for (n=0;n<form1.elements.length;n++){
        if (form1.elements[n].tabIndex==CurTabIndex){
            form1.elements[n].focus();
            return true;
            }
        }
    }
if (key==40){
    CurTabIndex=event.srcElement.tabIndex+1
    for (n=0;n<form1.elements.length;n++){
        if (form1.elements[n].tabIndex==CurTabIndex){
            form1.elements[n].focus();
            return true;
            }
        }
    }
}
</script>
</head>
<body>
<form name="form1" method="post" onkeyup="return go1()" language="javascript">
<input name=input1 type="text"><br>
<input name=input2 type="text"><br>
<input name=input3 type="text"><br>
…………
<input name=inputN type="text"><br>
</form>
<script>
form1.input1.focus();
for (n=0;n<form1.elements.length;n++){
    form1.elements[n].tabIndex=n;
}
</script>
</body>
</html>

原文转自:http://www.ltesting.net