IE 中 JavaScript 中的按键
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Internet Explorer 中,
这意味着,当您的嵌入式 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.