如何使用 jQuery 触发组合键
我编码了一些东西:
http://fincha.com/kunden/schmitt/
我放大与 .css("zoom")
但我需要按钮来模拟 CTRL + 或 CTRL -
此代码对我不起作用:
e = jQuery.Event("keydown");
e.which = 50;
$("input").trigger(e);
请帮忙!
编辑
我实际上想放大和缩小整个网页,而不仅仅是输入字段。
I have coded some stuff:
http://fincha.com/kunden/schmitt/
I zoom in with .css("zoom")
but I need the buttons to simulate CTRL + or CTRL -
This code isn't working for me:
e = jQuery.Event("keydown");
e.which = 50;
$("input").trigger(e);
Please help!
EDIT
I actually wanted to zoom-in
and zoom-out
the whole web page, not just a input field.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
jQuery 通过在
event
对象上设置一个或多个属性来标准化事件上的修饰键。因此,您想要设置event.ctrlKey
为true
,所以这应该适合您:但是,根据源代码的评论(下面链接):
因此,如果您在构造
Event
对象后无法设置事件的属性,那么您可以使用$.extend()
它来设置ctrlKey 属性:
另一件事:我不确定您是否尝试对 + 或 - 使用密钥代码
50
键。也许您是,并且您使用的是不同的键盘布局,但根据此演示,50
是点击 2 的 JavaScript 关键代码 - 因此这也可能是您问题的一部分。来源:jQuery API 页面上的评论。
编辑:
除此之外,我认为您实际上无法使用 JavaScript 更改浏览器的缩放级别,即使您“发送”键盘命令来执行此操作。
使用 JavaScript 访问浏览器的页面缩放控件
jQuery normalizes modifier keys on events by setting one or more properties on the
event
object. So, you want to setevent.ctrlKey
totrue
, so this should work for you:However, as per a comment at source (linked below):
So, if you're unable to set the event's properties after constructing the
Event
object, then you can$.extend()
it to set thectrlKey
property: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,50
is the JavaScript key code for hitting 2 - so that could also be part of your problem.Source: comments on a jQuery API page.
Edit:
All this aside, I don't think you can actually change the browser's zoom level using JavaScript, even if you're "sending" the keyboard command to do so.
Access browser's page zoom controls with javascript
来源:http://www. scottklarr.com/topic/126/how-to-create-ctrl-key-shortcuts-in-javascript/
这是针对 Ctrl+s 的,但您应该能够轻松修改它。
Source: http://www.scottklarr.com/topic/126/how-to-create-ctrl-key-shortcuts-in-javascript/
This is for Ctrl+s, but you should be able to modify it easily.