在文本框中输入完内容后,经常需要按回车,焦点跳到下个文本框,或者触发按钮事件,

 

判断是否按下的为回车非常简单:

function EnterPress(){
    if(event.keycode == 13){
        ...
    }
}

IE6的onkeypress会接受"回车事件",而onkeydown不会接受
IE8的onkeypress不会接受"回车事件",而onkeydown会接受
...不用纠结于此,两个都写上吧

<input type="text" onkeypress="EnterPress()" onkeydown="EnterPress()" />

但是,到了FF下面,又会出现矛盾.FF是onkeypress和onkeydown都接受"回车事件"的.
同时,为了兼容FF下面能获得event,需要这样写:

function EnterPress(e){   //传入 event
    var e = e | window.event;
    if(e.keycode == 13){
        ...
    }
}

那么,只要给任意的一个事件内传参数 event,另外一个不传参数,即可以让FF只执行一次了:

&<input type="text" onkeypress="EnterPress(event)" onkeydown="EnterPress()" />

 

综上,兼容IE和FF:

<head>
 <script>
 function EnterPress(e){   //传入 event
    var e = e | window.event;
    if(e.keycode == 13){
       document.getElementById("txtAdd").focus();
    }

 }
 </script>
</head>
<body>
 <input type="text"  />
 <input type="text"  />
</body>


 

--by:泡沫的幻想

相关文章:

  • 2022-03-05
  • 2022-12-23
  • 2021-09-02
  • 2021-07-15
  • 2022-02-18
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-02
  • 2021-06-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案