【问题标题】:event.shiftKey works in debugger but not at full speedevent.shiftKey 在调试器中工作但不是全速
【发布时间】:2018-03-25 18:38:59
【问题描述】:

我试图将输入字段限制为仅在此SO post 中记录的字符。 javascript 工作得很好,但正如这里和其他地方所指出的,当按下 shift 键时它不起作用。所以我可以按住 shift 键,输入 5 并显示一个“%”。所以我添加了这段代码来简单地返回 event.shiftKey = true 的 keydown 事件

$(document).on("keydown", ".numbers-only", function (event) {
            if(event.shiftKey){
                return
            }
            numbersOnly(event);
        }); 

这在我设置断点并使用 chrome 中的调试器单步执行时有效。但是,它不能实时工作,即使按住 shift 键也会调用 numbersOnly(event) 函数。

为什么函数在调试时会按原样返回,而不是实时返回?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    以下代码可以正常工作

    $(function() {
      $(document).on("keypress", function(event) {
        showChar(event)
      });
    
      function showChar(e) {
        if (e.shiftKey) {
          return
        }
        console.log(String.fromCharCode(e.charCode) + ": call function numbersOnly(e)");
      }
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div>
      <p>Press any character key, with or without holding down the SHIFT key.</p>
    </div>

    【讨论】:

    • 感谢您的回复,我同意代码应该可以工作。我认为它一定是 chrome/safari 的东西(我试过两个浏览器)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    • 2018-07-27
    • 1970-01-01
    • 2021-12-04
    • 2012-06-23
    • 1970-01-01
    相关资源
    最近更新 更多