开发者

Table onkeyup event is not firing in Firefox and Chrome

开发者 https://www.devze.com 2023-04-04 04:14 出处:网络
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/D

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消