在JavaScript中,如何创建自己的Keyboard事件? [英] In JavaScript, how can I create my own Keyboard event?

查看:156
本文介绍了在JavaScript中,如何创建自己的Keyboard事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中,如何创建自己的事件,可以将其分派给另一个函数.例如,我想创建Enter Key press event,以便我可以分派到另一个接受键盘事件的函数.

In JavaScript, how can I create my own event that I can dispatch to another function. For example, I want to create Enter Key press eventso I may dispatch to another function that accepts Keyboard events.

推荐答案

来自可以模拟密钥以编程方式新闻事件?

function simulateKeyPress(character) {
  jQuery.event.trigger({ type : 'keypress', which : character.charCodeAt(0) });
}

$(function() {
  $('body').keypress(function(e) {
    alert(e.which);
  });

  simulateKeyPress("e");
});

演示

DEMO

这篇关于在JavaScript中,如何创建自己的Keyboard事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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