在Jquery keydown函数上添加或语句的快速方法



假设我有一个类似的keydown元素,

$.ctrl('13', function(){

)}

这将得到ctrl+enter。但是,我也希望代码在Mac上运行command+enter。有没有一种方法,在不改变函数的情况下,在函数的开头有一个或语句,就像一样

$('.ctrl', '.metaKey')('13', function() {
)}

我想象这样的东西就像ctrl或metakey+enter。在Jquery中有办法做到这一点吗?

我想你想到的是event.ctrlKey,它是你正在听的任何键与控制键的组合。你可以这样访问它:

$(document).on('keypress', function(e) {
if (e.keyCode == 13 && e.ctrlKey)
alert("Ctrl + enter was pressed!!");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Click in this area to focus in this $(document) and press Ctrl+Enter
<?p>

https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/ctrlKeyhttps://api.jquery.com/category/events/event-object/

最新更新