给你个例子:<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<script>
function do_change(){
var cur_focus=hidden1.value;
var row=cur_focus.substring(1,2);
var col=cur_focus.substring(2,3);
if(event.keyCode==40&&row!=4){
eval("t"+(parseInt(row)+1)+col).focus();
hidden1.value="t"+(parseInt(row)+1)+col;
}else if(event.keyCode==38&&row!=1){
eval("t"+(parseInt(row)-1)+col).focus();
hidden1.value="t"+(parseInt(row)-1)+col;
}else if(event.keyCode==37&&col!=1){
eval("t"+row+(parseInt(col)-1)).focus();
hidden1.value="t"+row+(parseInt(col)-1);
}else if(event.keyCode==39&&col!=2){
eval("t"+row+(parseInt(col)+1)).focus();
hidden1.value="t"+row+(parseInt(col)+1);
}
}
function a(obj){
hidden1.value=obj.id;
}
function b(){
t11.focus();
}
</script>
</HEAD>
<BODY onkeydown="do_change()" onload="b()">
<table id="tab">
<tr>
<td><input type="text" name="t11" id="t11" onfocus="a(this)"></td>
<td><input type="text" name="t12" id="t12" onfocus="a(this)"></td>
</tr>
<tr>
<td><input type="text" name="t21" id="t21" onfocus="a(this)"></td>
<td><input type="text" name="t22" id="t22" onfocus="a(this)"></td>
</tr>
<tr>
<td><input type="text" name="t31" id="t31" onfocus="a(this)"></td>
<td><input type="text" name="t32" id="t32" onfocus="a(this)"></td>
</tr>
<tr>
<td><input type="text" name="t41" id="t41" onfocus="a(this)"></td>
<td><input type="text" name="t42" id="t42" onfocus="a(this)"></td>
</tr>
</table>
<input type=hidden id="hidden1" value="">
</BODY>
</HTML>
<HEAD>
<TITLE> New Document </TITLE>
<script>
function do_change(){
var cur_focus=hidden1.value;
var row=cur_focus.substring(1,2);
var col=cur_focus.substring(2,3);
if(event.keyCode==40&&row!=4){
eval("t"+(parseInt(row)+1)+col).focus();
hidden1.value="t"+(parseInt(row)+1)+col;
}else if(event.keyCode==38&&row!=1){
eval("t"+(parseInt(row)-1)+col).focus();
hidden1.value="t"+(parseInt(row)-1)+col;
}else if(event.keyCode==37&&col!=1){
eval("t"+row+(parseInt(col)-1)).focus();
hidden1.value="t"+row+(parseInt(col)-1);
}else if(event.keyCode==39&&col!=2){
eval("t"+row+(parseInt(col)+1)).focus();
hidden1.value="t"+row+(parseInt(col)+1);
}
}
function a(obj){
hidden1.value=obj.id;
}
function b(){
t11.focus();
}
</script>
</HEAD>
<BODY onkeydown="do_change()" onload="b()">
<table id="tab">
<tr>
<td><input type="text" name="t11" id="t11" onfocus="a(this)"></td>
<td><input type="text" name="t12" id="t12" onfocus="a(this)"></td>
</tr>
<tr>
<td><input type="text" name="t21" id="t21" onfocus="a(this)"></td>
<td><input type="text" name="t22" id="t22" onfocus="a(this)"></td>
</tr>
<tr>
<td><input type="text" name="t31" id="t31" onfocus="a(this)"></td>
<td><input type="text" name="t32" id="t32" onfocus="a(this)"></td>
</tr>
<tr>
<td><input type="text" name="t41" id="t41" onfocus="a(this)"></td>
<td><input type="text" name="t42" id="t42" onfocus="a(this)"></td>
</tr>
</table>
<input type=hidden id="hidden1" value="">
</BODY>
</HTML>
var myname=["txtKc","txtFj","txtCx","txtyb","txtss","txtgl","txtcz","txttp","txtkx","txtfy","txtwl","txtbl","txtpz","txthp","txtljbqj","txtLjbzq","txtsg","txtbp","txtgzw","txtqt","txtqh","txtgbj","txtSgsh","txtbz","txtaq","txtbx","txtkb","txthgyb","txthgyz"]; var nowfocus=1;
var myline=0;
document.all.txtKc1.focus();
function ceshi(tt)
{
if(tt==37)
{
if(nowfocus<2)
{
nowfocus=10;
if (myline<2) myline=myline;
else
myline-=1;
}
else nowfocus-=1;
var t=document.getElementById(myname[myline]+nowfocus);
t.focus();
}
else if(tt==38)
{
if(myline<1) myline=myline;
else myline-=1;
var t=document.getElementById(myname[myline]+nowfocus);
t.focus();
}
else if(tt==39)
{
if(nowfocus>9)
{
nowfocus=1;
if (myline>27) myline=myline;
else
myline+=1;
}
else nowfocus+=1;
var t=document.getElementById(myname[myline]+nowfocus);
t.focus();
}
else if(tt==40)
{
if(myline>27) myline=myline;
else myline+=1;
var t=document.getElementById(myname[myline]+nowfocus);
t.focus();
}
}</script>
定位就是光标移动到具体某一个text控件中,该text控件触发了onfocus事件(参考我的代码)你应该定义一个onfocus事件的函数,这个函数的作用就是记录光标当前在哪一个text控件中,具体到你的程序里面,我想应该是改变myline变量
/*
键盘上下键移动,在文本控件中移动
需要在 <body中 加上 onload="initKey()" >
cols =? 为多列设定,单列为 1 ,多列时可以使用左右键。。
2004/08/18 10:47:47
*/
var cols=2;
//var obj;
var CanMove=false;
var key; function initKey(){
document.onkeydown=keyDown;
document.onkeyup=keyUp;
} function keyDown(DnEvents){
var key=window.event.keyCode; // 如果按回车,且当前焦点不是button,submit,a ,image,则当做是Table键跳格
if(key ==13){
//alert (event.srcElement.type);
if (event.srcElement.type!='button' && event.srcElement.type!='submit' && event.srcElement.type!='reset' && event.srcElement.type!='image' && event.srcElement.type!=''){
event.keyCode =9;
}
}else{
if(key==116){
window.event.keyCode=0;
return false;
}
if(key==8){
if(event.srcElement.tagName!="INPUT"){
event.cancelBubble = true;
event.returnValue = false;
return false;
}
}
// 是否使用 Ctrl键 Start
//var IsCtrl=window.event.ctrlKey;
//if(!IsCtrl){
//return;
//}
// 是否使用 Ctrl键 End var obj = event.srcElement;
for(var i=0;i<document.forms[0].elements.length;i++){
if(document.forms[0].elements[i]==obj){
//alert (obj);
if (key == 37){//←
if(i>0){
document.forms[0].elements[i-1].focus();
}
}
if (key == 38){//↑
if(i>cols-1){ document.forms[0].elements[i-cols].focus(); }
}
if (key == 39){//→
if(i<document.forms[0].elements.length-1){ document.forms[0].elements[i+1].focus(); }
}
if (key == 40){//↓
if(i<document.forms[0].elements.length-cols){ document.forms[0].elements[i+cols].focus(); }
}
}
}
}} function keyUp(UpEvents){
return false;
}
/*<script language="javascript" for="document" event="onkeydown"><!-- if(event.keyCode==13 && event.srcElement.type!='button' && event.srcElement.type!='submit' && event.srcElement.type!='reset' && event.srcElement.type!='image' && event.srcElement.type!='') event.keyCode=9;--></script>
*/