如何在javascript中调用键盘函数 [英] How to call keyboard functions in javascript

查看:90
本文介绍了如何在javascript中调用键盘函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





使用以下代码,我们可以在按下键时调用一个函数。

 event.preventDefault()
在您捕获 F11事件
例如:
if (event.keyCode == 122 ){
event.preventDefault()
console.log(' F11被按下了);
}







但我需要点击按钮或调用javascript函数,键函数应该被调用,好像我按下了键。我想要反过来上面的例子。我们都知道ctrl + f用于查找文本。在我的应用程序中,当单击按钮时,应调用ctrl + f功能。因此,如果我单击按钮,搜索文本框应该显示在网页中,就像我按下了键一样。提前谢谢

解决方案

你可以使用jQuery甚至系统来假按键事件......

<前一行=Javascript> var e = jQuery.Event( keypress);
e.which = 122 ;


输入)。触发器(e);



输入是偶然会被解雇的女巫的元素 - 根据你的需要改变它......


Hi,

Using the following code,we can call a function when key is pressed.

event.preventDefault()
Right after you catch the "F11" event
e.g:
if(event.keyCode == 122) {
event.preventDefault()
console.log('F11 was pressed');
}




But I need when button is clicked or javascript function is called,key function should be called as if I pressed the key.Just I want the reverse of the above example.We all know ctrl+f is for finding the text.In my application,when button is clicked,ctrl+f feature should be called.Therefore if I click the button,search textbox should be displayed in the web page as if I pressed the key.Thanks in advance

解决方案

You can use jQuery even system to fake keypress events...

var e = jQuery.Event("keypress");
e.which = 122;


("input").trigger(e);


input is the element on witch the even will be fired - change it according to your need...


这篇关于如何在javascript中调用键盘函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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