【问题标题】:jquery keypress event object keyCode for firefox problem?jquery keypress event object keyCode for firefox问题?
【发布时间】:2011-07-29 07:39:21
【问题描述】:

FireFox 的 jQuery 按键事件为事件对象提供加密的 keyCode 属性 String.fromCharCode(e.keyCode) 转换后,但在 Chrome 中完美运行。

以下是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>

【问题讨论】:

    标签: javascript jquery events keypress keycode


    【解决方案1】:

    您应该在 Firefox 中使用 e.charCode

    $("#booter").keypress(function(e){
         var code = e.charCode || e.keyCode;
         var input = $(this).val() + String.fromCharCode(code);
         $('#text').focus().val(input);
         return false;
    });
    

    在这里试试:

    http://jsfiddle.net/REJ4t/

    PS 如果你想知道为什么这么乱:http://www.quirksmode.org/js/keys.html

    【讨论】:

    • 工作就像一个魅力......谢谢:)
    • e.charCode || e.keyCode
    • 请注意,根据 MDN e.keyCode 和 e.charCode 以及 e.which 已被弃用。我对此感到有点惊讶,因为名为 e.code 的替代品没有适当的支持。现在似乎唯一的选择是支持 IE9 的 e.key
    【解决方案2】:

    它适用于 IE 和 FF。

     $(document).ready(function (){
    
             $('#txtEntry').keypress(function (e) {
    
                 $('#lnkValidEdit').focus();
                 return false;
    
             });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-23
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      • 2015-06-17
      相关资源
      最近更新 更多