how to capture the key event from a view ?

后端 未结 2 1809
春和景丽
春和景丽 2020-12-03 11:57

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

myView = Backbone.View.extend({

  el: $(\'#someDiv\'),
  initialize: function(){
    // initial         


        
相关标签:
2条回答
  • 2020-12-03 12:40

    You can do this in the view initialize() function:

    _.bindAll(this, 'on_keypress');
    $(document).bind('keypress', this.on_keypress);
    
    0 讨论(0)
  • 2020-12-03 12:48

    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.

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

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

    0 讨论(0)
提交回复
热议问题