Firefox onkeydown检测按键 [英] Firefox onkeydown detect pressed key
问题描述
从输入文本我需要调用一个函数来捕获onkeydown事件,我需要传递一个参数到该事件,例如:
< input type =textonkeydown =TextOnKeyDown('myPrefix'); /> ...
然后在那个函数中我需要知道按了什么键。我使用
$ p $函数TextOnKeyDown(前缀)
...
}
不在Firefox中。我已经读过firefox,你必须传递一个参数给处理程序,然后用它来获得键,类似于:
< input type =textonkeydown =detectKey(event)/>
...
function detectKey(e){
var key =(document.all)? e.keyCode:e.which;
...
}
但我不能传递我的参数firefox所需的事件参数。任何建议吗?
没有建议的解决方案,因为我需要添加我自己的参数除了必要的事件参数为Firefox获取keyCode。无论如何,帮助,谢谢你们。我为未来的读者提供了最终的解决方案:
最后,我只需要将event参数添加到调用者:
< input type =textonkeydown =TextOnKeyDown('myPrefix',event); />
和处理程序:
function TextOnKeyDown(prefix,evt){
var key =(evt.which)? evt.which:evt.keyCode;
...
}
不知何故,不知道为什么。
From an input text I need to call a function to capture the onkeydown event and I need to pass a parameter to that event, for example:
<input type="text" onkeydown="TextOnKeyDown('myPrefix');" />...
Then in that function I need to know what key was pressed. I use
function TextOnKeyDown(prefix) {
var key = event.keyCode;
...
}
This works like a charm in IE, but not in Firefox. I've read that for firefox you have to pass an argument to the handler and then use it to get the key, something similar to:
<input type="text" onkeydown="detectKey(event)"/>
...
function detectKey(e){
var key = (document.all) ? e.keyCode : e.which;
...
}
But I cannot get to pass my parameter and the needed event parameter for firefox. Any suggestions?
None of the proposed solutions worked as what I needed was to add my own parameter apart from the necessary "event" parameter for Firefox to get the keyCode. Anyway the helped, thanks guys. I put the final solution for future readers:
In the end I just had to add the "event" parameter to the caller:
<input type="text" onkeydown="TextOnKeyDown('myPrefix', event);" />
and the handler:
function TextOnKeyDown(prefix, evt) {
var key = (evt.which) ? evt.which : evt.keyCode;
...
}
Somehow it didn't work before, I don't know why.
这篇关于Firefox onkeydown检测按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!