我想在页面加载时执行命令 ctrl + 0。这是我到目前为止所拥有的:
$( document ).ready(function() {
var press = jQuery.Event("keypress");
press.ctrlKey = true;
press.which = 48;
trigger(press);
});
这不会触发 ctrl+0,并且还会破坏页面上的另一个 div。
我做错了什么以及如何解决它?
最佳答案
您应该使用$(document).trigger(press)
,它将跟踪整个文档
上的keydowns
。
$(document).keydown(function(e) {
if (e.keyCode == 48 && e.ctrlKey)
$("body").append("<p>ctrl+0 Detected!</p>");
});
$(document).ready(function() {
var e = jQuery.Event("keydown");
e.keyCode = 48; // # Some key code value
e.ctrlKey = true;
$(document).trigger(e);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
关于javascript - 通过 jQuery 执行键盘命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34421255/