码迷,mamicode.com
首页 > 编程语言 > 详细

javascript 回车替换成TAB

时间:2016-10-14 23:32:12      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:

<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:&nbsp;&nbsp;&nbsp;&nbsp; <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>

 

javascript 回车替换成TAB

标签:

原文地址:http://www.cnblogs.com/gobuild/p/5962329.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!