本文介绍了onKeyUp没有为IE中的ESC触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,当按下时,我必须关闭对话框。我为此任务编写了以下简化代码示例:

I have a page where I have to close a dialog when is pressed. I wrote the following simplified code example for this task:

<html>
<head>
<script language="JavaScript">
function keyUpExample() {
alert('on' + event.type + ' event fired by ' + '"' + event.srcElement.id + '" ' + ' ' +        event.which) 
} 
</script>
</head>
<body id="myBody" onkeyup="keyUpExample()">
    Trying keyUp event: Press any key...
</body>
</html>

这在Chrome下可以正常使用,但在IE 7下无效。是否有任何解决方法可以订购应对这个问题?

This works as expected under Chrome but is NOT working under IE 7. Is there any workaround on order to cope this problem?

提前致谢!

推荐答案

重要事件不必被身体抓住,但文档或窗口可以跨浏览器工作。此外,keyCode为keyup或down事件返回正确的值。

Key events don't have to be caught by the body, but document or window works across browsers. Also, keyCode returns the correct value for keyup or down events.

<!doctype html>
<html lang="en">
<head>
<meta charset= "utf-8">
<title>Small Page</title>
<html>
<head>
<script>
function keyUpExample(e){
    e=e || window.event;
    var  who= e.target || e.srcElement;
    alert(e.type+' caught at '+who.nodeName+ ': key #'+e.keyCode) 
}
window.onload=function(){
    document.onkeyup= keyUpExample; 
    document.body.onkeyup= keyUpExample;    
}
</script>
</head>
<body>
    Trying keyUp event: Press any key...
</body>
</html>

这篇关于onKeyUp没有为IE中的ESC触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 23:20