jQuery 通过在事件上设置一个或多个属性来标准化事件上的修饰键event
目的。所以,你想设置event.ctrlKey http://www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html#Events-MouseEvent-ctrlKey to true
,所以这应该适合你:
e = jQuery.Event("keydown");
e.which = 50;
e.ctrlKey = true;
$("input").trigger(e);
然而,根据来源的评论(链接如下):
您无法轻松更改事件对象中的值(可能出于安全原因)。
因此,如果您在构建后无法设置事件的属性Event
对象,那么你可以$.extend()
它设置ctrlKey
财产:
e = jQuery.Event("keydown");
fake = $.extend({}, e, {which: 50, ctrlKey: true});
$("input").trigger(fake);
One other thing: I'm not sure if you're trying to use key code 50
for the + or the - keys. Maybe you are, and you're using a different keyboard layout, but according to this demo http://api.jquery.com/keydown/#target, 50
is the JavaScript key code for hitting 2 - so that could also be part of your problem.
Source: jQuery API 页面上的评论 http://api.jquery.com/keydown/#comment-72949775.
Edit:
除了所有这些,我认为你不能actually使用 JavaScript 更改浏览器的缩放级别,即使您是“发送”键盘命令来执行此操作。
使用 javascript 访问浏览器的页面缩放控件 https://stackoverflow.com/questions/2578354/access-browsers-page-zoom-controls-with-javascript