本文介绍如何在JavaScript中处理文本框的回车键事件,实现按回车自动聚焦到下一个输入框的功能。通过EnterPress函数结合onkeypress和onkeydown事件,确保在IE和Firefox等主流浏览器中兼容运行。
JS实现文本框回车触发确定并兼容IE与Firefox
代码如下:
<head>
<script>
function EnterPress(e) { // 传入 event
var e = e || window.event;
if (e.keyCode == 13) {
document.getElementById("txtB").focus();
}
}
</script>
</head>
<body>
<input type="text" id="txtA" onkeypress="EnterPress(event)" onkeydown="EnterPress()" />
<input type="text" id="txtB" />
</body>