<table>
<tr><td><input onkeypress="addNewLine(this)"></td></tr></table>
<script>
function addNewLine(it) {
  if (event.keyCode == 13) {
    var thisLine = it;
    while (thisLine.tagName != "TR") {
      thisLine = thisLine.parentNode;
    }
    var newLine = thisLine.cloneNode(true);
    var inputbox = newLine.firstChild.firstChild
    inputbox.value = "";
    thisLine.parentNode.appendChild(newLine);
    inputbox.focus();
  }
}
</script>

解决方案 »

  1.   

    <table>
    <tr><td><input onkeypress="addNewLine(this)"></td></tr></table>
    <script>
    function addNewLine(it) {
      if (event.keyCode == 13) {
        var thisLine = it;
        while (thisLine.tagName != "TR") {
          thisLine = thisLine.parentNode;
        }
        var newLine = thisLine.cloneNode(true);
        var inputbox = newLine.firstChild.firstChild
        inputbox.value = "";
        thisLine.parentNode.appendChild(newLine);
        inputbox.focus();
        //----------forbid event for other line---
        it.onkeypress = null
        //----------------------------------------
      }
    }
    </script>
      

  2.   

    师兄,你的两个我都试了,我在table的tr中多加一个<td><input></td>,代码还是在第一个input得到回车后,就换行了。显然还需要改进呀,谢谢
      

  3.   

    This is only a example, just to tell you that your problem can be solved in this way. It is very easy to modify it by yourself, isn't it?
      

  4.   

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>无标题文档</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <script language="javascript">
    function addRow(src){
    //alert(src);
    var newrow = src.insertRow(src.rows.length-1);
    newrow.height=20;
    var i=4;
    while(i--){
    var newcell = newrow.insertCell();
    switch(i){
    case 0: newcell.innerHTML= '<input type="button" onClick="javascript:delRow(this.parentElement.parentElement)" value="删除此行">';break;
    default: newcell.innerHTML=div1.innerHTML;break;
    }
    }
    newrow.cells[0].children[0].focus();
    }
    function delRow(src){
    var tab=src.parentElement;
    var i=tab.rows.length;
    while(i--){
    if(src==tab.rows[i]){
    alert("就是这行----"+ i);
    tab.deleteRow(i);
    }
    }
    }function nextRow(obj)
    { if(event.keyCode==13)
    {
    if(obj.cellIndex != 2)
    {
    event.keyCode = 9;
    }
    else
    {
    addRow(obj.parentElement.parentElement);
    }
    }
    }
    </script>
    </head><body>
    <table id="tb" width="100%"  border="1" align="center" cellpadding="1" cellspacing="1" style="border-collapse:collapse" bordercolor="#111111">
      <tr>
        <th scope="col" width="25%">奶类</th>
        <th scope="col" width="25%">数量</th>
        <th scope="col" width="25%">总重</th>
        <th scope="col" width="25%">单价</th>
      </tr>
      <tr id="blankRow" onClick="addRow(this.parentElement)">
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    <div id="div1" style="display:none "><input id="txt" type="text" style="width:97%; background-color:#FFFFEF" onkeydown="javascript:return nextRow(this.parentElement)"></div>
    </body>
    </html>