[英]How to listen the key pressed on all the document using JavaScript and Meteor template?
I have founded that if I want to listen on all the document I should do : 我发现如果我想听所有文件,我应该这样做:
$(document).keydown(function(e) {
console.log(e);
console.log(e.keyCode);
if (e.keyCode == 27) {
$('#tftextinput').value="";
$('#tfbutton').click();
}
});
but it doesn't write anything in the console... So I have tried an other version like this: 但是它在控制台中什么也没写...所以我尝试了其他这样的版本:
$(".container.body").keydown(function(e) {
console.log(e);
console.log(e.keyCode);
if (e.keyCode == 27) {
$('#tftextinput').value="";
$('#tfbutton').click();
}
});
this code is in the $(document).ready(function() {});
这段代码在
$(document).ready(function() {});
but nothing happened too... 但是什么也没发生...
EDIT: 编辑:
If I write this code in the web console it works: 如果我在Web控制台中编写此代码,则可以正常工作:
So why it doesn't work in my Meteor template code ? 那么,为什么它在我的Meteor模板代码中不起作用?
Template.home.onRendered(function() {
$(document).ready(function() {
/*
this method listen if we press "enter" in the research field and click on the button
*/
$('#tftextinput').keypress(function(e) {
if (e.keyCode == 13) {
$('#tfbutton').click();
}
});
$(document).keydown(function(e) {
console.log(e);
console.log(e.keyCode);
if (e.keyCode == 27) {
$('#tftextinput').value="";
$('#tfbutton').click();
}
});
});
});
the first listener works (the one who listens tftextinput
) 第一个监听器起作用(监听
tftextinput
)
You could use Template events to do the same: 您可以使用模板事件执行以下操作:
Template.home.events({
'keydown':function(event){
...
},
'keypress #tftextinput': function(event){
...
}
});
Try on window 试一试
$(window).on("keydown",function(e) {
console.log(e);
console.log(e.keyCode);
if (e.keyCode == 27) {
$('#tftextinput').value="";
$('#tfbutton').click();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.