日期:2014-05-16  浏览次数:20327 次

方向键控制光标能定位
一个页面上全是文本框让用户输入数据的,加了点代码能让文本框里用上下键控制光标,但当点另外的方本框后用上下键时又回到上一次用上下键移动的地方,想点到一个地方后,用上下键时,就从点的那个位置开始,怎么办,请帮忙

------解决方案--------------------
给你个例子:

<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>
------解决方案--------------------
我有代码
/*
键盘上下键移动,在文本控件中移动
需要在 <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;