Mozilla中,document.onkeydown不起作用

shanghr 2004-09-20 02:05:19
function document.onkeydown()
{
if (event.keyCode==13) {document.getElementById("ImageButton2").click()}
}

Mozilla中,document.onkeydown不起作用,该怎么写,才能生效呢?
...全文
418 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
rootcn 2004-09-20
  • 打赏
  • 举报
回复
captureevent()
wangxiaomax 2004-09-20
  • 打赏
  • 举报
回复
<html>
<head>
<title>Keyboard Event Handler Lab</title>
<style type="text/css">
td {text-align:center}
</style>
<script type="text/javascript">
function init() {
document.onkeydown = showKeyDown;
document.onkeyup = showKeyUp;
document.onkeypress = showKeyPress;
}
function showKeyDown(evt) {
evt = (evt) ? evt : window.event;
document.getElementById("pressKeyCode").innerHTML = 0;
document.getElementById("upKeyCode").innerHTML = 0;
document.getElementById("pressCharCode").innerHTML = 0;
document.getElementById("upCharCode").innerHTML = 0;
restoreModifiers("");
restoreModifiers("Down");
restoreModifiers("Up");
document.getElementById("downKeyCode").innerHTML = evt.keyCode;
if (evt.charCode) {
document.getElementById("downCharCode").innerHTML = evt.charCode;
}
showModifiers("Down", evt);
}
function showKeyUp(evt) {
evt = (evt) ? evt : window.event;
document.getElementById("upKeyCode").innerHTML = evt.keyCode;
if (evt.charCode) {
document.getElementById("upCharCode").innerHTML = evt.charCode;
}
showModifiers("Up", evt);
return false;
}
function showKeyPress(evt) {
evt = (evt) ? evt : window.event;
document.getElementById("pressKeyCode").innerHTML = evt.keyCode;
if (evt.charCode) {
document.getElementById("pressCharCode").innerHTML = evt.charCode;
}
showModifiers("", evt);
return false;
}
function showModifiers(ext, evt) {
restoreModifiers(ext);
if (evt.shiftKey) {
document.getElementById("shift" + ext).style.backgroundColor =
"#ff0000";
}
if (evt.ctrlKey) {
document.getElementById("ctrl" + ext).style.backgroundColor =
"#00ff00";
}
if (evt.altKey) {
document.getElementById("alt" + ext).style.backgroundColor =
"#0000ff";
}
}
function restoreModifiers(ext) {
document.getElementById("shift" + ext).style.backgroundColor =
"#ffffff";
document.getElementById("ctrl" + ext).style.backgroundColor =
"#ffffff";
document.getElementById("alt" + ext).style.backgroundColor =
"#ffffff";
}
</script>
</head>
<body onload="init()">
<h1>Keyboard Event Handler Lab</h1>
<hr />
<form ID=Form1>
<table border="2" cellpadding="2">
<tr>
<th></th>
<th>onKeyDown</th>
<th>onKeyPress</th>
<th>onKeyUp</th>
</tr>
<tr>
<th>Key Codes</th>
<td id="downKeyCode">0</td>
<td id="pressKeyCode">0</td>
<td id="upKeyCode">0</td>
</tr>
<tr>
<th>Char Codes (IE5/Mac; NN6)</th>
<td id="downCharCode">0</td>
<td id="pressCharCode">0</td>
<td id="upCharCode">0</td>
</tr>
<tr>
<th rowspan="3">Modifier Keys</th>
<td><span id="shiftDown">Shift</span></td>
<td><span id="shift">Shift</span></td>
<td><span id="shiftUp">Shift</span></td>
</tr>
<tr>
<td><span id="ctrlDown">Ctrl</span></td>
<td><span id="ctrl">Ctrl</span></td>
<td><span id="ctrlUp">Ctrl</span></td>
</tr>
<tr>
<td><span id="altDown">Alt</span></td>
<td><span id="alt">Alt</span></td>
<td><span id="altUp">Alt</span></td>
</tr>
</table>
</form>
</body>
</html>
wangxiaomax 2004-09-20
  • 打赏
  • 举报
回复
不好意思,我上面说的是错的。
不过下面一段话,我在mozilla中测试通过。
<input name="test" onkeydown="if (event.keyCode==13) {alert('test');}">
不知document应该怎么用?
wangxiaomax 2004-09-20
  • 打赏
  • 举报
回复
document.onkeypress

try

87,919

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧