您的位置:程序门 -> web 开发 -> javascript



方向键控制光标能定位


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


方向键控制光标能定位[已结贴,结贴人:ssdxk]
发表于:2007-08-25 10:25:39 楼主
一个页面上全是文本框让用户输入数据的,加了点代码能让文本框里用上下键控制光标,但当点另外的方本框后用上下键时又回到上一次用上下键移动的地方,想点到一个地方后,用上下键时,就从点的那个位置开始,怎么办,请帮忙
发表于:2007-08-25 10:35:211楼 得分:0
定义一个全局变量记录光标的位置就行了
发表于:2007-08-25 10:38:512楼 得分:10
给你个例子:

<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>
发表于:2007-08-25 10:42:583楼 得分:0
谢谢了,我测试了一下能用,看看加我上边能用不,谢谢啊,一会给分
发表于:2007-08-25 10:52:154楼 得分:0
你把你的代码贴上来,我帮你改改
发表于:2007-08-25 11:07:305楼 得分:0
太长了我分了n页也放不上去,很郁闷,有好几百个文本框,郁闷
发表于:2007-08-25 11:09:116楼 得分:0
<script>
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>
发表于:2007-08-25 11:10:137楼 得分:0
这是以前写的,只能控制光标移动,不能定位后移动,你帮忙看下吧,谢谢
发表于:2007-08-25 11:41:518楼 得分:0
学习     &&   up
发表于:2007-08-25 12:09:039楼 得分:0
不能定位后移动,那是因为你的定位根本就没有操作
定位就是光标移动到具体某一个text控件中,该text控件触发了onfocus事件(参考我的代码)

你应该定义一个onfocus事件的函数,这个函数的作用就是记录光标当前在哪一个text控件中,具体到你的程序里面,我想应该是改变myline变量
发表于:2007-08-25 12:55:5210楼 得分:0
哦谢谢了,我再看看
发表于:2007-08-25 13:35:5211楼 得分:10
我有代码
/*
键盘上下键移动,在文本控件中移动
需要在   <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>
*/
发表于:2007-08-25 13:36:1712楼 得分:0
使用上面代码即可.
发表于:2007-08-25 18:25:5213楼 得分:0
学习+顶


快速检索

最新资讯
热门点击