event.shiftKey works in debugger but not at full speed
我试图将输入字段限制为仅此SO文章中记录的字符。 该javascript效果很好,但是如此处和其他地方所述,当按下Shift键时,它不起作用。 因此我可以按住Shift键,输入5并显示"%"。 因此,我添加了此代码以仅返回keydown事件,其中event.shiftKey = true
1 2 3 4 5 6 | $(document).on("keydown",".numbers-only", function (event) { if(event.shiftKey){ return } numbersOnly(event); }); |
当我设置断点并逐步调试chrome中的调试器时,此方法有效。 但是,它不能实时工作,即使按住Shift键也可以调用numberOnly(event)函数。
为什么函数在调试时会按原样返回,而不是实时返回?
以下代码可以正常工作
1 2 3 4 5 6 7 8 9 10 11 12 | $(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)"); } }) |
1 2 3 4 5 | <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> <p> Press any character key, with or without holding down the SHIFT key. </p> |