js获取键盘按键响应事件(兼容各浏览器)

<script type="text/javascript" language="JavaScript" charset="UTF-8">

    document.onkeydown = function(event) {

        var e = event || window.event || arguments.callee.caller.arguments[0]; //事件

        var thisElement = e.srcElement || e.target; //事件源

        //alert(e.keyCode);按键 键码值

        if (e && e.keyCode == 27) { // 按 Esc 

            //要做的事情

        }

        if (e && e.keyCode == 113) { // 按 F2 

            var srcElement = "";

            srcElement += " \n" + "Element.id : " + thisElement.id;

            srcElement += " \n" + "Element.tagName : " + thisElement.tagName;

            srcElement += " \n" + "Element.type : " + thisElement.type;

            srcElement += " \n" + "Element.value : " + thisElement.value;

            srcElement += " \n" + "Element.name : " + thisElement.name;

            srcElement += " \n" + "Element.className : " + thisElement.className;

            srcElement += " \n" + "Element.parentElement.id : " + thisElement.parentElement.id;

            srcElement += " \n" + "Element.parentElement.className : " + thisElement.parentElement.className;

            srcElement += " \n" + "Element.parentElement.classList : " + thisElement.parentElement.classList;

            srcElement += " \n" + "Element.getattribute : " + thisElement.getAttribute;

            alert(srcElement);

            //alert($(thisElement).parent().html());

        }

        if (e && e.keyCode == 13) { // enter 键 

            try {

                //屏蔽IE

                e.keyCode = 0;

                e.returnValue = false;

                //屏蔽火狐

                e.preventDefault();

                e.stopPropagation();

            } catch(ex) {}

        }

    };

</script>

 

你可能感兴趣的:(浏览器)