javascript当中 document onkeydown的用法

例 2.2(documentKeypressIEFF.html)
马克-to-win:当系统看见这句话:document.onkeydown = handleKeypress; 以后,当你按keydown时,系统自然就调用: handleKeypress(event)。而且传进来event参数。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</HEAD>

<script>
    function handleKeypress(event)
    {
/*火狐用event所以window.event为undefined,ie正相反,所以event || window.event可以兼容*/
        alert("event is "+event +"window.event is "+window.event);
        alert(111||undefined);//任何数和undefined做||,为原值。
        var event = event || window.event;

        if (window.navigator.userAgent.indexOf("MSIE") >= 1)    
        {
            var key = event.keyCode;
        alert("Key: " + String.fromCharCode(key) + "\nCharacter code: " + key + ".");        
        }            
        else if (          window.navigator.userAgent.indexOf("Firefox") >= 1)            
        {                   var key = event.which;//event.which获取按下的键盘按键Unicode值:
        /*fromCharCode() 可接受一个或n个指定的 Unicode 值,然后返回一个或多个字符*/
        alert("Key: " + String.fromCharCode(key) + "\nCharacter code: " + key + ".");
        }

    }
    document.onkeydown = handleKeypress;
</script>
</HTML>

更多内容请见原文,文章转载自:https://blog.csdn.net/qq_43650923/article/details/100513755

posted @ 2021-11-25 21:06  malala  阅读(574)  评论(0编辑  收藏  举报