js中的键盘事件:onkeydown、onkeypress、onkeyup

≡放荡痞女 提交于 2019-12-08 02:15:42
相关代码:
<!doctype html>
<html>
    <head>
        <!--声明当前页面编码集(中文编码<gbk,gb2312>,国际编码<utf-8>)-->
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta name="keywords" content="关键词,关键词">
        <meta name="description" content="">
        <title> html </title>
        <style type="text/css">
            *{padding:0px;margin:0px;}
        </style>
    </head>
<body>
    <input type="text" id="txt"/>
    <script>
        /*
            onkeydown----->键盘按下去的事件
            onkeyup------->键盘抬起了的事件

            radio checkbox  focus();
        */
        var oTxt = document.getElementById("txt");
        //oTxt.onkeyup = function(){
            //alert(1);
            //alert(this.value);
        //}


        /*
            onkeydown 在 onkeypress 触发之前
            onkeypress 只会响应字母数字符号,其他的例如F1-F12、ctrl/shift/alt等不会响应

        */
        document.onkeypress = function(){
            console.log(2);
        };
        document.onkeydown = function(){
            console.log(1);
        };



    </script>
</body>

</html>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!