如何使用jQuery触发键组合 [英] How to trigger key combo with jQuery

查看:130
本文介绍了如何使用jQuery触发键组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一些内容:

http: //fincha.com/kunden/schmitt/

我用.css(缩放)放大但我需要按钮来模拟CTRL +或CTRL -

I zoom in with .css("zoom") but I need the buttons to simulate CTRL + or CTRL -

此代码对我不起作用:

e = jQuery.Event("keydown");        
e.which = 50;       
$("input").trigger(e);

请帮助!

编辑

我只是不想缩小和缩小漏洞页面

I just wont to zoomin and zoomout the hole page

推荐答案

jQuery规范化通过在事件对象上设置一个或多个属性来修改事件的键。所以,你想设置 event.ctrlKey true ,所以这应该适合你:

jQuery normalizes modifier keys on events by setting one or more properties on the event object. So, you want to set event.ctrlKey to true, so this should work for you:

e = jQuery.Event("keydown");        
e.which = 50;
e.ctrlKey = true;
$("input").trigger(e);

但是,根据来源评论(链接如下):

However, as per a comment at source (linked below):


您无法轻易更改事件对象中的值(可能出于安全原因)。

You cannot easily change values in the event object (probably for security reasons).

因此,如果您在构建事件对象后无法设置事件的属性,那么您可以 $。extend()设置 ctrlKey 属性:

So, if you're unable to set the event's properties after constructing the Event object, then you can $.extend() it to set the ctrlKey property:

e = jQuery.Event("keydown");
fake = $.extend({}, e, {which: 50, ctrlKey: true});
$("input").trigger(fake);






另一件事:我不确定是否您正在尝试为 + - 键使用键代码 50 。也许你是,并且你正在使用不同的键盘布局,但根据此演示 50 是用于点击 2 的JavaScript密钥代码 - 因此这也可能是您问题的一部分。


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.

资料来源:评论一个jQuery API页面

编辑:

除此之外,我认为你不能实际使用JavaScript改变浏览器的缩放级别,即使你是发送键盘命令这样做。

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.

访问浏览器页面缩放控件使用javascript

这篇关于如何使用jQuery触发键组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆