【问题标题】:how to stop keypress event in keydown如何在keydown中停止按键事件
【发布时间】:2009-09-14 13:33:33
【问题描述】:

如何在 keydown 处理程序中停止按键事件。

【问题讨论】:

标签: javascript-events


【解决方案1】:

即使从 keydown 开始,您也无法停止按键...它们都是非常独立的事件。您可以做的是在读取字符后取消 keydown / keypress。这是我只允许在文本框 (jQuery) 中输入字母和数字的方法

  urlBox.keypress(function(e){
            if(e.which == 13 || e.which == 8 || e.which == 0) return true;
            if(48 <= e.which && e.which <= 57) return true;
            if(65 <= e.which && e.which <= 90) return true;
            if(97 <= e.which && e.which <= 122) return true;
            return false;                 
          });

【讨论】:

  • 我不认为它依赖于浏览器。无论如何,如果您想安全起见,也可以调用 prevent default 。查找相关文档。
猜你喜欢
  • 2022-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多