IE 中 JavaScript 中的按键

发布于 2024-10-19 16:47:13 字数 2184 浏览 3 评论 0原文

我有一个 Flex 应用程序。它在 HTML 页面上的播放器中运行。我需要捕获按键事件并防止 IE 浏览器按其预期运行。这是一些代码:

实际上,玩家放置的部分..

<html>
<head>
</head>

<body scroll="no" onkeydown=keypress(event)>
<noscript>
    <object id="app" width="100%" height="100%" onkeydown="keypress(event)" onkeypress="keypress(event)">
        //some params
    </object>
</noscript>
</body>
</html>

这是一个,我尝试以不同的方式捕获关键输入:

<script language="JavaScript" type="text/javascript">

        function keypress(e) {
            alert("Hello from keypress");
        }

        function init() {
            //index
            alert("Init!!!");
            document.getElementById('app').onkeydown = function() {
                alert("Key Pressed - 1");
            };
            document.onkeydown = function() {
                alert("Key Pressed - 2");
            };

            document.getElementById('app').onkeypress = function() {
                alert("Key Pressed - 3")
            };

            document.onkeypress = function() {
                alert("Key Pressed - 4")
            };

            window.onkeydown = function() {
                alert("Key Pressed - 5");
            };
            window.onkeypress = function() {
                alert("Key Pressed - 6");
            };

            document.body.onkeypress = function() {
                alert("Key Pressed - 7")
            };

            document.body.onkeydown = function() {
                alert("Key Pressed - 8")
            };

            if (window.addEventListener) {
                window.addEventListener('keypress', keypress, false);
            } else if (window.attachEvent) {
                window.attachEvent('onkeypress', keypress);
            } else {
                window.onkeypress = keypress;
            }
        }


        window.onload = init;
        document.onload = init;
    </script>

我不打算将它们全部使用在一起,只是将它们全部收集起来以显示你知道我已经尝试了几乎所有的方法(也包括所有带有 1 个参数的方法)。

问题是我收到的唯一警报是“Init!!!”。它有什么问题或者我做错了什么?

任何帮助将不胜感激。

编辑:我在播放器加载其内容之前收到“Init”消息..也许问题出在某个地方?

I have a Flex application. It running in a player on the HTML-page. I need catch the key press events and prevent IE browser from acting like it wants. Here's some code:

Actually, the part where player was layed ..

<html>
<head>
</head>

<body scroll="no" onkeydown=keypress(event)>
<noscript>
    <object id="app" width="100%" height="100%" onkeydown="keypress(event)" onkeypress="keypress(event)">
        //some params
    </object>
</noscript>
</body>
</html>

And here's the one, where I'm trying in a different ways to catch key input:

<script language="JavaScript" type="text/javascript">

        function keypress(e) {
            alert("Hello from keypress");
        }

        function init() {
            //index
            alert("Init!!!");
            document.getElementById('app').onkeydown = function() {
                alert("Key Pressed - 1");
            };
            document.onkeydown = function() {
                alert("Key Pressed - 2");
            };

            document.getElementById('app').onkeypress = function() {
                alert("Key Pressed - 3")
            };

            document.onkeypress = function() {
                alert("Key Pressed - 4")
            };

            window.onkeydown = function() {
                alert("Key Pressed - 5");
            };
            window.onkeypress = function() {
                alert("Key Pressed - 6");
            };

            document.body.onkeypress = function() {
                alert("Key Pressed - 7")
            };

            document.body.onkeydown = function() {
                alert("Key Pressed - 8")
            };

            if (window.addEventListener) {
                window.addEventListener('keypress', keypress, false);
            } else if (window.attachEvent) {
                window.attachEvent('onkeypress', keypress);
            } else {
                window.onkeypress = keypress;
            }
        }


        window.onload = init;
        document.onload = init;
    </script>

I wasn't going to use them all together, just gathered them all to show you that I've tried almost everything (also including all this with 1 parameter).

The problem is that the only Alert I'm getting is "Init!!!". What's wrong with it or what I'm doing wrong?

Any help would be greatly appreciated.

Edit: I get the "Init" message before the player loads it's content .. maybe the problem is somewhere there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与酒说心事 2024-10-26 16:47:13

在 Internet Explorer 中,元素中的嵌入式控件内发生的事件不会触发 DOM 对象上的等效事件。它们由嵌入式控件使用,并且控件有责任相应地处理它们。

这意味着,当您的嵌入式 Flex 应用程序处于焦点状态时,您的 JavaScript 代码将无法处理任何关键事件。

In Internet Explorer, events that occur within an embedded control in the <object> element do not fire equivalent events on the DOM object. They are consumed by the embedded control, and it is that control's responsibility to handle them accordingly.

This means, when your embedded Flex application is focussed, your JavaScript code will not be able to handle any key events.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文