Javascript window.event.keyCode 如何在 Firefox 上做到这一点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6116933/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
window.event.keyCode how to do it on Firefox?
提问by lisovaccaro
I'm using this code to check for keydown and display the string 'Pressed' while a key is down.
我正在使用此代码来检查 keydown 并在键按下时显示字符串 'Pressed'。
<body onKeyDown="doKey(window.event.keyCode)" onKeyUp="doKey2(window.event.keyCode)">
<script>
function doKey($key) {
document.getElementById('keydown').innerHTML='Pressed';
}
function doKey2($key) {
document.getElementById('keydown').innerHTML='';
}
</script>
<div id="keydown"></div>
The problem is that for some reason it's only working on Chrome. I think the 'window.event.keyCode' doesn't return anything. How do I make it work at least in Firefox too? Thanks
问题是由于某种原因它只能在 Chrome 上运行。我认为“window.event.keyCode”不会返回任何内容。我如何让它至少在 Firefox 中也能工作?谢谢
回答by Guffa
Some browsers have a global event object, other send the event object to the event handler as a parameter. Chrome and Internet Exlporer uses the former, Firefox uses the latter.
一些浏览器有一个全局事件对象,另一些浏览器将事件对象作为参数发送给事件处理程序。Chrome 和 Internet Exlporer 使用前者,Firefox 使用后者。
Some browsers use keyCode
, others use charCode
.
一些浏览器使用keyCode
,其他浏览器使用charCode
.
Use arguments[0]
to pick up the event object sent as a parameter, and if there is no object there, there is a global object instead:
使用arguments[0]
拿起作为参数发送事件的对象,如果没有对象那里,有一个全局对象,而不是:
onkeydown="doKey(arguments[0] || window.event)"
In the method you can check for either keyCode
or charCode
在该方法中,您可以检查keyCode
或charCode
function doKey(event) {
var key = event.keyCode || event.charCode;
...
}
Note the lowercase name onkeydown
. If you are using XHTML event names has to be lowercase, or the browser might ignore it.
请注意小写名称onkeydown
。如果您使用 XHTML 事件名称必须是小写的,否则浏览器可能会忽略它。
回答by silverfox
<html>
<body onKeyDown="doKey(event)" onKeyUp="doKey2(event)">
<script>
function doKey(e) {
evt = e || window.event; // compliant with ie6
document.getElementById('keydown').innerHTML = evt.keyCode+' Pressed';
}
function doKey2(e) {
document.getElementById('keydown').innerHTML='';
}
</script>
<div id="keydown"></div>
</body>
</html>
If we passed event
as parameter, most modern browsers will work well. I have tested with Chrome 12, Firefox 4, IE 7/8/9.
如果我们event
作为参数传递,大多数现代浏览器都可以正常工作。我已经用 Chrome 12、Firefox 4、IE 7/8/9 进行了测试。
回答by Ray K.
I've run into the same problem, as I'm sure thousands of coders have.
我遇到了同样的问题,因为我相信成千上万的编码员都有。
The problem is that the browsers (other than IE) don't like window.event.
问题是浏览器(IE 除外)不喜欢 window.event。
I'm poking around trying to find a solution (which is how I stumbled across this), and I found the following (so far):
我正在四处寻找解决方案(这是我偶然发现的),我发现了以下内容(到目前为止):
1) Write JQuery:
1)编写JQuery:
$(document).keyup(function(e) {
var GetKey = e.keyCode;
`enter code here`
}
});
2) Redefine the key variable:
2)重新定义关键变量:
var key = (window.event) ? evt.keyCode : evt.which;
I tried the JQuery solution. It seems to be okay in FF, but I ran into an unexpected bug in IE that I'm still trying to solve. I haven't yet tried the second solution; that's next, if I can't get the JQuery to work.
我尝试了 JQuery 解决方案。在 FF 中似乎没问题,但我在 IE 中遇到了一个我仍在尝试解决的意外错误。我还没有尝试过第二种解决方案;接下来,如果我不能让 JQuery 工作。