如何捕获从视图中的关键事件? [英] how to capture the key event from a view ?

查看:111
本文介绍了如何捕获从视图中的关键事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从视图中捕捉到关键事件如下:

I'm trying to capture the key event from a view as follows:

myView = Backbone.View.extend({

  el: $('#someDiv'),
  initialize: function(){
    // initialize some subviews
  },
  render: function(){
    return this;
  },
  events:{
   'keypress #someDiv': 'showKey'
  },
  showKey: function(e){
    console.log(e.keyCode);
  }
})

这不起作用?

PS:在视图或子视图有没有[输入]元素。我只需要知道,如果用户presses任意键,然后做一些事情的看法。

ps: There a no [input] elements in the view or its subviews. I just need to know if the user presses any key and then do something on the view.

推荐答案

重点pressed转到页上的焦点元素。如果你在你看来什么都没有,并认为没有任何焦点,那么你将不会有任何键preSS事件。

Key pressed goes to the focused element on the page. If you have nothing in your view and the view does not have any focus, then you will not have any key press events.

(顺便说一句,如果你想为this.el做重点preSS事件,做到重点preSS:showKey)

( btw if you want to do key press event for this.el, do "keypress" : "showKey" )

在你上面code身体将最有可能获得的所有关键preSS事件。

In you above code the body will most likely receive all keypress events.

这篇关于如何捕获从视图中的关键事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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