急用 我想做一个表格要求支持方向键和回车键,当光标在最后一行最后一列时,按下回车键自动添加新的行。帮帮忙吧!谢谢了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //创建列表对象var list = { CallBack:"", obj:{}, init:function(){ this.obj = document.getElementById("MainTable"); this.obj.childNodes(0).childNodes(1).style.display = "none"; this.add(); }, add: function(){ var Row = this.obj.insertRow() ; Row.align = "center" ; var Len = this.obj.childNodes(0).childNodes(0).childNodes.length ; var Col ,Inp ; //增加列 for (var i=0 ; i<Len ;i++) { Col = Row.insertCell(); Inp = this.obj.childNodes(0).childNodes(1).childNodes(i); Col.innerHTML = Inp.innerHTML ; Col.innerHTML = Col.innerHTML.replace("</ITEM>" ,"") ; if (i == Len - 1) { this.eve(Col.childNodes(0) ,"onkeydown" , this.e13 , i ,1) } else { this.eve(Col.childNodes(0) ,"onkeydown" , this.e13 , i ,0) } } //定位光标 var TempObj = this.obj.rows(this.obj.rows.length-1).childNodes(0).childNodes(0); TempObj.focus() //回调函数 if(typeof this.CallBack == "function") { this.CallBack(TempObj); } }, e13: function(thisobj , index ,sn) { //回车键 if(event.keyCode == 13) { if (sn == 1) { if(thisobj.parentNode.parentNode.nextSibling == null) { list.add(); } } if(event != null) { event.returnValue = false; } } //左方向键 else if(event.keyCode == 37) { if(thisobj.parentNode.previousSibling != null) { thisobj.parentNode.previousSibling.childNodes(0).focus(); } } //右方向键 else if(event.keyCode == 39) { event.keyCode = 9 ; } //上方向键 else if(event.keyCode == 38) { if(thisobj.parentNode.parentNode.previousSibling != null) { if(thisobj.parentNode.parentNode.previousSibling.style.display != "none") { thisobj.parentNode.parentNode.previousSibling.childNodes(index).childNodes(0).focus(); } } } //下方向键 else if(event.keyCode == 40) { if(thisobj.parentNode.parentNode.nextSibling != null) { thisobj.parentNode.parentNode.nextSibling.childNodes(index).childNodes(0).focus(); } } else { return; } }, //注册事件 eve: function(obj, ex , func ,i ,sn){ obj.setAttribute( ex , function(){ return func(obj , i , sn) ; } ) ; }}; 试试看<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> <title>dhtml.table.controlByKeyboard.html</title> <meta name="generator" content="editplus" /> <meta name="author" content="Gao YiXiang" /> <meta name="email" content="[email protected]" /> <meta name="keywords" content="javascript dhtml dom" /> <meta name="description" content="I love web development." /> <style type="text/css">table{ border-collapse: collapse; border: 1px solid blue;}th, td{ border: 1px solid blue; } </style></head><body> <h3>根据键盘操作表格!注:IE6sp1 测试可用!FF2 不兼容。</h3> tab:切换单元格。<br /> shift + tab:向上切换单元格。<br /> 上下方向键:换行。<br /> 左右方向键:文本框内移动光标。<br /> <table id="tbeTest"> <tr> <th>姓名</th> <th>年龄</th> <th>爱好</th> </tr> <tr> <td> <input type="text" style="width: 100px;" value="yixianggao" /> </td> <td> <input type="text" style="width: 50px; text-align: center;" value="29" /> </td> <td> <input type="text" style="width: 150px;" value="Web Development" /> </td> </tr> <tr> <td> <input type="text" style="width: 100px;" /> </td> <td> <input type="text" style="width: 50px;" /> </td> <td> <input type="text" style="width: 150px;" /> </td> </tr> </table> <script type="text/javascript"> <!--function $(sId){ return document.getElementById(sId);}/* * 设置 键盘事件。 * 上下键换行、回车增加新行。 */function setCellKeyEvents(oTarget){ var oCell; for (var i=0; i<oTarget.cells.length; i++) { oCell = oTarget.cells[i]; oCell.onkeydown = function() { switch (event.keyCode) { case upKeyCode: moveToPreviousRow(this); break; case downKeyCode: moveToNextRow(this); break; case enterKeyCode: appendNewRow(this); break; } }; }}/* * 上移一行。 */function moveToPreviousRow(oTd){ var startRowIndex = 1; var curRowIndex = oTd.parentNode.rowIndex; var curCellIndex = oTd.cellIndex; if (curRowIndex != startRowIndex) { oTd.parentNode.previousSibling.cells[curCellIndex].firstChild.focus(); }}/* * 下移一行。 */function moveToNextRow(oTd){ var endRowIndex = oTd.parentNode.parentNode.rows.length - 1; var curRowIndex = oTd.parentNode.rowIndex; var curCellIndex = oTd.cellIndex; if (curRowIndex != endRowIndex) { oTd.parentNode.nextSibling.cells[curCellIndex].firstChild.focus(); }}/* * 清空文本输入框。 */function clearTextBoxValve(oTarget){ var cInput = oTarget.getElementsByTagName("input"); for (var i=0; i<cInput.length; i++) { if (cInput[i].type.toLowerCase() == "text") { cInput[i].value = ""; } }}/* * 添加新行。 */function appendNewRow(oTd){ var endRowIndex = oTd.parentNode.parentNode.rows.length - 1; var endCellIndex = oTd.parentNode.cells.length - 1; var curRowIndex = oTd.parentNode.rowIndex; var curCellIndex = oTd.cellIndex; if (curRowIndex == endRowIndex && curCellIndex == endCellIndex) { var lastRow = oTd.parentNode.cloneNode(true); clearTextBoxValve(lastRow); oTd.parentNode.parentNode.appendChild(lastRow); setCellKeyEvents(lastRow); lastRow.firstChild.firstChild.focus(); }}var upKeyCode = 38;var downKeyCode = 40;var enterKeyCode = 13;var oTbe = $("tbeTest");setCellKeyEvents(oTbe); //--> </script></body></html>送你三本手册!DHTML参考手册http://download.csdn.net/source/308913样式表中文手册http://download.csdn.net/source/304124JScript语言参考http://download.csdn.net/source/308916 微调了一下<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> <title>dhtml.table.controlByKeyboard.html</title> <meta name="generator" content="editplus" /> <meta name="author" content="Gao YiXiang" /> <meta name="email" content="[email protected]" /> <meta name="keywords" content="javascript dhtml dom" /> <meta name="description" content="I love web development." /> <style type="text/css">table{ border-collapse: collapse; border: 1px solid blue;}th, td{ border: 1px solid blue; } </style></head><body> <h3>根据键盘操作表格!注:IE6sp1 测试可用!FF2 不兼容。</h3> tab:正向切换单元格。<br /> shift + tab:反向切换单元格。<br /> 上下方向键:换行。<br /> 左右方向键:文本框内移动光标。<br /> <table id="tbeTest"> <tr> <th>姓名</th> <th>年龄</th> <th>爱好</th> </tr> <tr> <td> <input type="text" style="width: 100px;" value="yixianggao" /> </td> <td> <input type="text" style="width: 50px; text-align: center;" value="29" /> </td> <td> <input type="text" style="width: 150px;" value="Web Development" /> </td> </tr> <tr> <td> <input type="text" style="width: 100px;" /> </td> <td> <input type="text" style="width: 50px; text-align: center;" /> </td> <td> <input type="text" style="width: 150px;" /> </td> </tr> </table> <script type="text/javascript"> <!--function $(sId){ return document.getElementById(sId);}/* * 设置 键盘事件。 * 上下键换行、回车增加新行。 */function setCellKeyEvents(oTarget){ var upKeyCode = 38; var downKeyCode = 40; var enterKeyCode = 13; var oCell; for (var i=0; i<oTarget.cells.length; i++) { oCell = oTarget.cells[i]; oCell.onkeydown = function() { switch (event.keyCode) { case upKeyCode: moveToPreviousRow(this); break; case downKeyCode: moveToNextRow(this); break; case enterKeyCode: appendNewRow(this); break; } }; }}/* * 上移一行。 */function moveToPreviousRow(oTd){ var startRowIndex = 1; var curRowIndex = oTd.parentNode.rowIndex; var curCellIndex = oTd.cellIndex; if (curRowIndex != startRowIndex) { oTd.parentNode.previousSibling.cells[curCellIndex].firstChild.focus(); }}/* * 下移一行。 */function moveToNextRow(oTd){ var endRowIndex = oTd.parentNode.parentNode.rows.length - 1; var curRowIndex = oTd.parentNode.rowIndex; var curCellIndex = oTd.cellIndex; if (curRowIndex != endRowIndex) { oTd.parentNode.nextSibling.cells[curCellIndex].firstChild.focus(); }}/* * 清空文本输入框。 */function clearTextBoxValve(oTarget){ var cInput = oTarget.getElementsByTagName("input"); for (var i=0; i<cInput.length; i++) { if (cInput[i].type.toLowerCase() == "text") { cInput[i].value = ""; } }}/* * 添加新行。 */function appendNewRow(oTd){ var endRowIndex = oTd.parentNode.parentNode.rows.length - 1; var endCellIndex = oTd.parentNode.cells.length - 1; var curRowIndex = oTd.parentNode.rowIndex; var curCellIndex = oTd.cellIndex; if (curRowIndex == endRowIndex && curCellIndex == endCellIndex) { var lastRow = oTd.parentNode.cloneNode(true); clearTextBoxValve(lastRow); oTd.parentNode.parentNode.appendChild(lastRow); setCellKeyEvents(lastRow); lastRow.firstChild.firstChild.focus(); }}var oTbe = $("tbeTest");setCellKeyEvents(oTbe); //--> </script></body></html> 怎么多了个<TBODY> 大家帮忙看下我这个程序为什么实现不了 javascript 如何动态改变图片地址 dom 文本对象模型的问题,轻松获取100分。 请问用应该什么时候用GET方法,什么时候用POST方法 有没办法把js文件一次加载后,存起来,下次不用加载? 对话框脚本 如何打开一个新的窗口,同时让这个新窗口的关闭按纽不好用呀? 大家看看!!这个问题怎么办? 为什么我的node.js中supervisor不是内部或外部命令 javascript中如何获取表格中的当前行号? [求助,急]请教各位大侠,如何实现网站的用户分类注册和登录?
//创建列表对象
var list = {
CallBack:"",
obj:{},
init:function(){
this.obj = document.getElementById("MainTable");
this.obj.childNodes(0).childNodes(1).style.display = "none";
this.add();
},
add: function(){
var Row = this.obj.insertRow() ;
Row.align = "center" ;
var Len = this.obj.childNodes(0).childNodes(0).childNodes.length ;
var Col ,Inp ;
//增加列
for (var i=0 ; i<Len ;i++)
{
Col = Row.insertCell();
Inp = this.obj.childNodes(0).childNodes(1).childNodes(i);
Col.innerHTML = Inp.innerHTML ;
Col.innerHTML = Col.innerHTML.replace("</ITEM>" ,"") ;
if (i == Len - 1)
{
this.eve(Col.childNodes(0) ,"onkeydown" , this.e13 , i ,1)
}
else
{
this.eve(Col.childNodes(0) ,"onkeydown" , this.e13 , i ,0)
} }
//定位光标
var TempObj = this.obj.rows(this.obj.rows.length-1).childNodes(0).childNodes(0);
TempObj.focus()
//回调函数
if(typeof this.CallBack == "function")
{
this.CallBack(TempObj);
}
},
e13: function(thisobj , index ,sn)
{
//回车键
if(event.keyCode == 13)
{
if (sn == 1)
{
if(thisobj.parentNode.parentNode.nextSibling == null)
{
list.add();
}
}
if(event != null)
{
event.returnValue = false;
}
}
//左方向键
else if(event.keyCode == 37)
{
if(thisobj.parentNode.previousSibling != null)
{
thisobj.parentNode.previousSibling.childNodes(0).focus();
}
}
//右方向键
else if(event.keyCode == 39)
{
event.keyCode = 9 ;
}
//上方向键
else if(event.keyCode == 38)
{
if(thisobj.parentNode.parentNode.previousSibling != null)
{
if(thisobj.parentNode.parentNode.previousSibling.style.display != "none")
{
thisobj.parentNode.parentNode.previousSibling.childNodes(index).childNodes(0).focus();
}
}
}
//下方向键
else if(event.keyCode == 40)
{
if(thisobj.parentNode.parentNode.nextSibling != null)
{
thisobj.parentNode.parentNode.nextSibling.childNodes(index).childNodes(0).focus();
}
}
else
{
return;
}
},
//注册事件
eve: function(obj, ex , func ,i ,sn){
obj.setAttribute(
ex ,
function(){
return func(obj , i , sn) ;
}
) ;
}
};
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>dhtml.table.controlByKeyboard.html</title>
<meta name="generator" content="editplus" />
<meta name="author" content="Gao YiXiang" />
<meta name="email" content="[email protected]" />
<meta name="keywords" content="javascript dhtml dom" />
<meta name="description" content="I love web development." />
<style type="text/css">
table
{
border-collapse: collapse;
border: 1px solid blue;
}
th, td
{
border: 1px solid blue;
}
</style>
</head>
<body>
<h3>根据键盘操作表格!注:IE6sp1 测试可用!FF2 不兼容。</h3>
tab:切换单元格。<br />
shift + tab:向上切换单元格。<br />
上下方向键:换行。<br />
左右方向键:文本框内移动光标。<br />
<table id="tbeTest">
<tr>
<th>姓名</th>
<th>年龄</th>
<th>爱好</th>
</tr>
<tr>
<td>
<input type="text" style="width: 100px;" value="yixianggao" />
</td>
<td>
<input type="text" style="width: 50px; text-align: center;" value="29" />
</td>
<td>
<input type="text" style="width: 150px;" value="Web Development" />
</td>
</tr>
<tr>
<td>
<input type="text" style="width: 100px;" />
</td>
<td>
<input type="text" style="width: 50px;" />
</td>
<td>
<input type="text" style="width: 150px;" />
</td>
</tr>
</table>
<script type="text/javascript">
<!--
function $(sId)
{
return document.getElementById(sId);
}/*
* 设置 键盘事件。
* 上下键换行、回车增加新行。
*/
function setCellKeyEvents(oTarget)
{
var oCell;
for (var i=0; i<oTarget.cells.length; i++)
{
oCell = oTarget.cells[i];
oCell.onkeydown = function()
{
switch (event.keyCode)
{
case upKeyCode:
moveToPreviousRow(this);
break;
case downKeyCode:
moveToNextRow(this);
break;
case enterKeyCode:
appendNewRow(this);
break;
}
};
}
}
/*
* 上移一行。
*/
function moveToPreviousRow(oTd)
{
var startRowIndex = 1;
var curRowIndex = oTd.parentNode.rowIndex;
var curCellIndex = oTd.cellIndex; if (curRowIndex != startRowIndex)
{
oTd.parentNode.previousSibling.cells[curCellIndex].firstChild.focus();
}
}
/*
* 下移一行。
*/
function moveToNextRow(oTd)
{
var endRowIndex = oTd.parentNode.parentNode.rows.length - 1;
var curRowIndex = oTd.parentNode.rowIndex;
var curCellIndex = oTd.cellIndex; if (curRowIndex != endRowIndex)
{
oTd.parentNode.nextSibling.cells[curCellIndex].firstChild.focus();
}
}
/*
* 清空文本输入框。
*/
function clearTextBoxValve(oTarget)
{
var cInput = oTarget.getElementsByTagName("input");
for (var i=0; i<cInput.length; i++)
{
if (cInput[i].type.toLowerCase() == "text")
{
cInput[i].value = "";
}
}
}
/*
* 添加新行。
*/
function appendNewRow(oTd)
{
var endRowIndex = oTd.parentNode.parentNode.rows.length - 1;
var endCellIndex = oTd.parentNode.cells.length - 1;
var curRowIndex = oTd.parentNode.rowIndex;
var curCellIndex = oTd.cellIndex; if (curRowIndex == endRowIndex
&& curCellIndex == endCellIndex)
{
var lastRow = oTd.parentNode.cloneNode(true);
clearTextBoxValve(lastRow);
oTd.parentNode.parentNode.appendChild(lastRow);
setCellKeyEvents(lastRow);
lastRow.firstChild.firstChild.focus();
}
}var upKeyCode = 38;
var downKeyCode = 40;
var enterKeyCode = 13;var oTbe = $("tbeTest");setCellKeyEvents(oTbe);
//-->
</script>
</body>
</html>
送你三本手册!DHTML参考手册
http://download.csdn.net/source/308913样式表中文手册
http://download.csdn.net/source/304124JScript语言参考
http://download.csdn.net/source/308916
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>dhtml.table.controlByKeyboard.html</title>
<meta name="generator" content="editplus" />
<meta name="author" content="Gao YiXiang" />
<meta name="email" content="[email protected]" />
<meta name="keywords" content="javascript dhtml dom" />
<meta name="description" content="I love web development." />
<style type="text/css">
table
{
border-collapse: collapse;
border: 1px solid blue;
}
th, td
{
border: 1px solid blue;
}
</style>
</head>
<body>
<h3>根据键盘操作表格!注:IE6sp1 测试可用!FF2 不兼容。</h3>
tab:正向切换单元格。<br />
shift + tab:反向切换单元格。<br />
上下方向键:换行。<br />
左右方向键:文本框内移动光标。<br />
<table id="tbeTest">
<tr>
<th>姓名</th>
<th>年龄</th>
<th>爱好</th>
</tr>
<tr>
<td>
<input type="text" style="width: 100px;" value="yixianggao" />
</td>
<td>
<input type="text" style="width: 50px; text-align: center;" value="29" />
</td>
<td>
<input type="text" style="width: 150px;" value="Web Development" />
</td>
</tr>
<tr>
<td>
<input type="text" style="width: 100px;" />
</td>
<td>
<input type="text" style="width: 50px; text-align: center;" />
</td>
<td>
<input type="text" style="width: 150px;" />
</td>
</tr>
</table>
<script type="text/javascript">
<!--
function $(sId)
{
return document.getElementById(sId);
}/*
* 设置 键盘事件。
* 上下键换行、回车增加新行。
*/
function setCellKeyEvents(oTarget)
{
var upKeyCode = 38;
var downKeyCode = 40;
var enterKeyCode = 13; var oCell;
for (var i=0; i<oTarget.cells.length; i++)
{
oCell = oTarget.cells[i];
oCell.onkeydown = function()
{
switch (event.keyCode)
{
case upKeyCode:
moveToPreviousRow(this);
break;
case downKeyCode:
moveToNextRow(this);
break;
case enterKeyCode:
appendNewRow(this);
break;
}
};
}
}
/*
* 上移一行。
*/
function moveToPreviousRow(oTd)
{
var startRowIndex = 1;
var curRowIndex = oTd.parentNode.rowIndex;
var curCellIndex = oTd.cellIndex; if (curRowIndex != startRowIndex)
{
oTd.parentNode.previousSibling.cells[curCellIndex].firstChild.focus();
}
}
/*
* 下移一行。
*/
function moveToNextRow(oTd)
{
var endRowIndex = oTd.parentNode.parentNode.rows.length - 1;
var curRowIndex = oTd.parentNode.rowIndex;
var curCellIndex = oTd.cellIndex; if (curRowIndex != endRowIndex)
{
oTd.parentNode.nextSibling.cells[curCellIndex].firstChild.focus();
}
}
/*
* 清空文本输入框。
*/
function clearTextBoxValve(oTarget)
{
var cInput = oTarget.getElementsByTagName("input");
for (var i=0; i<cInput.length; i++)
{
if (cInput[i].type.toLowerCase() == "text")
{
cInput[i].value = "";
}
}
}
/*
* 添加新行。
*/
function appendNewRow(oTd)
{
var endRowIndex = oTd.parentNode.parentNode.rows.length - 1;
var endCellIndex = oTd.parentNode.cells.length - 1;
var curRowIndex = oTd.parentNode.rowIndex;
var curCellIndex = oTd.cellIndex; if (curRowIndex == endRowIndex
&& curCellIndex == endCellIndex)
{
var lastRow = oTd.parentNode.cloneNode(true);
clearTextBoxValve(lastRow);
oTd.parentNode.parentNode.appendChild(lastRow);
setCellKeyEvents(lastRow);
lastRow.firstChild.firstChild.focus();
}
}var oTbe = $("tbeTest");setCellKeyEvents(oTbe); //-->
</script>
</body>
</html>