日期:2014-05-16 浏览次数:20410 次
<!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>回车切换焦点,兼容FF</title> <script type="text/javascript"> document.onkeydown=function(event){ var e=event||window.event; var keyCode=e.keyCode||e.which;//e.which 兼容FF var target=e.srcElement||e.target; if (keyCode ==13) { var tabindex=target.tabIndex+1; if( tabindex==6) tabindex=1; document.getElementById('elem'+tabindex).focus(); } } </script> </head> <body> <form> <input type="text" tabindex="1" id="elem1" /> <input type="text" value="readonly" readonly="readonly" tabindex="-1" /> <input type="button" value="click" tabindex="2" id="elem2" /> <input type="text" tabindex="3" id="elem3" /> <select tabindex="4" id="elem4" > <option value="1">select</option> <option value="2">select2</option> </select> <input type="button" value="submit" tabindex="5" id="elem5" /> </form> </body> </html>
------解决方案--------------------
<!doctype html> <html> <head> <title>Test</title> </head> <body> <form id="myform"> <input type="text" id="elem1" /> <input type="text" value="readonly" readonly="readonly" tabIndex="-1" /> <input type="button" value="click" id="elem2" /> <input type="text" id="elem3" /> <select id="elem4" > <option value="1">select</option> <option value="2">select2</option> </select> <input type="button" value="submit" id="elem5" /> </form> <script> var form = document.getElementById("myform"), elems = form.getElementsByTagName("*"), controls = []; for(var i = 0, len = elems.length; i < len; i++){ if(elems[i].tabIndex !== -1 && elems[i].nodeName !== "OPTION"){ controls.push(elems[i]); } } function indexOf(arr, valu