如何从视图中捕获关键事件?
我试图从视图中捕获关键事件,如下所示:
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: 视图或其子视图中没有 [input] 元素。我只需要知道用户是否按下任意键,然后在视图上执行某些操作。
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);
}
})
That does not work ?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在视图的initialize()函数中执行此操作:
You can do this in the view initialize() function:
按下的按键将转到页面上的焦点元素。如果视图中没有任何内容并且视图没有任何焦点,那么您将不会有任何按键事件。
(顺便说一句,如果您想为 this.el 执行按键事件,请执行“keypress”:“showKey”)
在上面的代码中,主体很可能会收到所有按键事件。
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.