Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
本机javascript中的Keypress函数_Javascript_Jquery - Fatal编程技术网

本机javascript中的Keypress函数

本机javascript中的Keypress函数,javascript,jquery,Javascript,Jquery,下面是我的代码: $(document).keypress(function(e){ var s = String.fromCharCode(e.which); if ( s === "f"){ alert ("its f"); } else if ( s === "r"){ alert ("its r"); } }); 我想在JavaScript中完成此操作 我该怎么做呢?试试这个 document.onkeypress = keyPressFunct

下面是我的代码:

$(document).keypress(function(e){
  var s = String.fromCharCode(e.which);
  if ( s === "f"){
    alert ("its f");
  }
  else if ( s === "r"){
    alert ("its r");
  }
});
我想在JavaScript中完成此操作

我该怎么做呢?

试试这个

  document.onkeypress = keyPressFunction;

    function keyPressFunction(e) {
      var s = String.fromCharCode(e.which);
        if ( s === "f"){
         alert ("its f");
        }
        else if ( s === "r"){
         alert ("its r");
        }
    }

在原生js中,您可以通过onClick、onKeyPress等附加事件。。。etc元素属性。 不要忘记,事件处理程序函数中的事件参数;) 在不同的浏览器中,您可以通过事件的不同属性读取按键的代码

以下是一个例子:

<script type="text/javascript">
  document.getElementsByTagName("body")[0].onkeypress=function(e){
       var s = String.fromCharCode(e.which | evt.witch );
       switch(s){
           case "f":
              alert("key is 'f'");
              break;
           ....
       }
   };
</script>
您应该在加载文档后运行这些脚本。您可以像这样订阅
domready
事件:

<script type="text/javascript">
     document.getElementsByTagName("body")[0].addEventListener('keypress',function(e){
       var s = String.fromCharCode(e.which | evt.witch );
       switch(s){
           case "f":
              alert("key is 'f'");
              break;
           ....
       }
     });
</script>
<script type="text/javascript">
       document.addEventListener( "DOMContentLoaded", function(){
           //your code here  (one of the above)
       }, false );
</script>
<script>  
   (function() {
        var jQ = document.createElement('script'); jQ.type = 'text/javascript'; 
        jQ.async = true;
        jQ.src = '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(jQ, s);
   })();
</script>

正如谷歌在analitycs代码中所做的;)

这里有一个简单的函数,它完全满足您的需要-

document.onkeydown=function(e){
    var char= String.fromCharCode(e.keyCode);
    if(char=='f'){
        alert(char);
    }else if(char=='r'){
        alert(char);
    }
}

为什么标题是“本地javascript中的Document ready函数”?不确定他为什么一开始就将其命名为“Document ready”,但这篇文章会回答:无法设置Undefinedd的属性“onkeypress”,您将代码粘贴到文档的末尾?您的代码是否在domready事件之后运行?我编辑它,以显示如何在domready之后运行defined@EnterJQ我更新了我的代码,检查一下键盘上的按键事件属性是,使用(按下)和按键(释放)。
document.onkeydown=function(e){
    var char= String.fromCharCode(e.keyCode);
    if(char=='f'){
        alert(char);
    }else if(char=='r'){
        alert(char);
    }
}