Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.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/73.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 jQuery运行函数_Javascript_Jquery - Fatal编程技术网

Javascript 使用.keypress jQuery运行函数

Javascript 使用.keypress jQuery运行函数,javascript,jquery,Javascript,Jquery,我有一些功能。比如说 <script> //Function1 alert('Function 1 run') //Function2 alert('Function 2 run') </script> 那么你们会给我什么建议呢?事件键将捕获Ctrl键并保持该值为真。尽管这样做有效,但我认为最好使用一些插件,因为可能会出现许多浏览器兼容性问题 $document.on'keydown',functionevent{ ifevent.ctrlKey&&event.

我有一些功能。比如说

<script>

//Function1
alert('Function 1 run')

//Function2
alert('Function 2 run')

</script>
那么你们会给我什么建议呢?

事件键将捕获Ctrl键并保持该值为真。尽管这样做有效,但我认为最好使用一些插件,因为可能会出现许多浏览器兼容性问题

$document.on'keydown',functionevent{ ifevent.ctrlKey&&event.which==65{ 警报“Ctrl+A”; 违约事件; } ifevent.ctrlKey&&event.which==66{ 警报“Ctrl+B”; 违约事件; } } 试试这个

$(document).keypress(function(e) {
    if(e.ctrlKey && e.key == "a") {
        alert('function 1');
    }
    if(e.ctrlKey && e.key == "b") {
        alert('function 2');
    }
});

您是否尝试了$document.on'keypress…,和。您可以将jquery插件用作热键。jskeypress不适用于非打印键,如Ctrl。您必须改用keydown/keydup
$(document).keypress(function(e) {
    if(e.ctrlKey && e.key == "a") {
        alert('function 1');
    }
    if(e.ctrlKey && e.key == "b") {
        alert('function 2');
    }
});