javascript jquery keypress 事件对象 keyCode for firefox 问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6869996/
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
jquery keypress event object keyCode for firefox problem?
提问by Mr Coder
jQuery keypress event for FireFox gives encrypted keyCode
property for event object
after String.fromCharCode(e.keyCode)
conversion but works perfect in Chrome.
FireFox 的 jQuery 按键事件keyCode
在String.fromCharCode(e.keyCode)
转换后为事件对象提供加密属性,但在 Chrome 中工作完美。
Following is the javascript code:
以下是javascript代码:
<!-- #booter and #text are ids of html element textarea -->
<script type="text/javascript">
$(function(){
$('#booter').keypress(function(e){
var input = $(this).val() + String.fromCharCode(e.keyCode);
$('#text').focus().val(input);
return false;
});
});
</script>
回答by mamoo
You should use e.charCode
in Firefox.
您应该e.charCode
在 Firefox 中使用。
$("#booter").keypress(function(e){
var code = e.charCode || e.keyCode;
var input = $(this).val() + String.fromCharCode(code);
$('#text').focus().val(input);
return false;
});
Try it here:
在这里试试:
PS If you're wondering why all this mess: http://www.quirksmode.org/js/keys.html
PS 如果你想知道为什么会有这么多的混乱:http: //www.quirksmode.org/js/keys.html
回答by Mohsin Razuan
It works for both IE & FF.
它适用于 IE 和 FF。
$(document).ready(function (){
$('#txtEntry').keypress(function (e) {
$('#lnkValidEdit').focus();
return false;
});