Following code works fine with IE, but doesn't work with other browsers.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTM开发者_StackOverflow中文版L 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function OnLoad() {
var tbl = document.getElementById("tbl");
tbl.addEventListener("keyup", OnKeyUp);
tbl.focus();
}
function OnKeyUp(event) {
alert(event.keyCode);
}
</script>
</head>
<body onload="OnLoad()">
<table id="tbl">
<tr>
<td>
1
</td>
<td>
2
</td>
<td>
3
</td>
</tr>
<tr>
<td>
4
</td>
<td>
5
</td>
<td>
6
</td>
</tr>
</table>
</body>
</html>
The problem is that the table
element cannot be given focus in some browsers. If you add a tabindex
property to the table, your code should work, as that will allow the table to receive focus:
<table id="tbl" tabindex="1">
<!--Rest of your code-->
</table>
Here's a working example.
精彩评论