js控制光标上下移动
2016-06-30 09:35
363 查看
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="javascript">
var cols=3;
var obj;
var key;
function setobj(input){
obj=input;
}
function init(){
document.onkeydown=keyDown;
document.onkeyup=keyUp;
}
//按键
function keyDown(DnEvents){
key=window.event.keyCode;
//f5键
if(key==116){
//取消按键
window.event.keyCode=0;
return false;
}
//backplace
if(key==8){
//触发事件的控件 既不是 <input />,该语句值在IE里有效
if(event.srcElement.tagName!="INPUT"){
//可以停止事件继续上传
event.cancelBubble = true;
//取消事件默认行为
event.returnValue = false;
return false;
}
}
for(var i=0;i<document.forms[0].elements.length;i++){
if(document.forms[0].elements[i]==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 < 2*cols -1){
document.forms[0].elements[i + 1].focus();
key = 0;
}
}
//下键
if (key == 40){//↓
if(i< cols){
document.forms[0].elements[cols + i].focus();
}
}
}
}
}
function keyUp(UpEvents){
return false;
}
</script>
</head>
<body bgcolor="#FFFFFF" text="#000000" onload="init()">
<form>
<table border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td>
<input type="text" name="textfield" onfocus="setobj(this)">
</td>
<td>
<input type="text" name="textfield2" onfocus="setobj(this)">
</td>
<td>
<input type="text" name="textfield3" onfocus="setobj(this)">
</td>
</tr>
<tr>
<td>
<input type="text" name="textfield5" onfocus="setobj(this)">
</td>
<td>
<input type="text" name="textfield6" onfocus="setobj(this)">
</td>
<td>
<input type="text" name="textfield7" onfocus="setobj(this)">
</td>
</tr>
</table>
</form>
</body>
</html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="javascript">
var cols=3;
var obj;
var key;
function setobj(input){
obj=input;
}
function init(){
document.onkeydown=keyDown;
document.onkeyup=keyUp;
}
//按键
function keyDown(DnEvents){
key=window.event.keyCode;
//f5键
if(key==116){
//取消按键
window.event.keyCode=0;
return false;
}
//backplace
if(key==8){
//触发事件的控件 既不是 <input />,该语句值在IE里有效
if(event.srcElement.tagName!="INPUT"){
//可以停止事件继续上传
event.cancelBubble = true;
//取消事件默认行为
event.returnValue = false;
return false;
}
}
for(var i=0;i<document.forms[0].elements.length;i++){
if(document.forms[0].elements[i]==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 < 2*cols -1){
document.forms[0].elements[i + 1].focus();
key = 0;
}
}
//下键
if (key == 40){//↓
if(i< cols){
document.forms[0].elements[cols + i].focus();
}
}
}
}
}
function keyUp(UpEvents){
return false;
}
</script>
</head>
<body bgcolor="#FFFFFF" text="#000000" onload="init()">
<form>
<table border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td>
<input type="text" name="textfield" onfocus="setobj(this)">
</td>
<td>
<input type="text" name="textfield2" onfocus="setobj(this)">
</td>
<td>
<input type="text" name="textfield3" onfocus="setobj(this)">
</td>
</tr>
<tr>
<td>
<input type="text" name="textfield5" onfocus="setobj(this)">
</td>
<td>
<input type="text" name="textfield6" onfocus="setobj(this)">
</td>
<td>
<input type="text" name="textfield7" onfocus="setobj(this)">
</td>
</tr>
</table>
</form>
</body>
</html>
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- java-WEB中的监听器Lisener
- Android学习笔记(二九):嵌入浏览器
- Android java 与 javascript互访(相互调用)的方法例子
- GUI - Web前端开发框架
- Extjs4.0 最新最全视频教程
- JavaScript演示排序算法
- javascript实现10进制转为N进制数
- MyEclipse Web Project转Eclipse Dynamic Web Project
- axis备忘
- 最后一次说说闭包
- Ajax
- 2019年开发人员应该学习的8个JavaScript框架
- HTML中的script标签研究
- 对一个分号引发的错误研究
- 异步流程控制:7 行代码学会 co 模块
- ES6 走马观花(ECMAScript2015 新特性)
- JavaScript拆分字符串时产生空字符的原因