<head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head> <script lang="javascript"> function check() { var name = document.getElementById("name").value; var password = document.getElementById("password").value; alert("name="+name+" and password="+password); } // rewrite the ‘onkeydown‘ event to make sure ‘Enter‘ key can be used as ‘Tab‘ in some needed circumstances. It work fine on both IE and non-IE browsers. document.onkeydown=function(evt) { var isie = (document.all) ? true : false; var key; var srcobj; // if the agent is an IE browser, it‘s easy to do this. if (isie) { key = event.keyCode; srcobj=event.srcElement; } else { key = evt.which; srcobj=evt.target; } if(key==13 && srcobj.type!=‘button‘ && srcobj.type!=‘submit‘ &&srcobj.type!=‘reset‘ && srcobj.type!=‘textarea‘ && srcobj.type!=‘‘) { if(isie) event.keyCode=9; else { var el=getNextElement(evt.target); if (el.type!=‘hidden‘) ; //nothing to do here. else while(el.type==‘hidden‘) el=getNextElement(el); if(!el) return false; else el.focus(); } } } function getNextElement(field) { var form = field.form; for(var e = 0; e < form.elements.length; e++) { if (field == form.elements[e]) break; } return form.elements[++e % form.elements.length]; } </script> <body> <p><b>javascript 回车替换成TAB</b></p> <form> <table width="20%"> <tr> <td> name: <input type="text" id="name" size="12"> </td> </tr> <tr> <td> password: <input type="password" size="12" id="password"> </td> </tr> <tr> <td> <input type="button" value="Submit" onclick="check()"> </td> </tr> <table> <form> </body> </html> </html>
时间: 2024-10-10 18:04:10