Cancel the keydown in HTML

后端 未结 6 924
清歌不尽
清歌不尽 2020-11-29 10:28

How can I cancel the keydown of a specific key on the keyboard, for example(space, enter and arrows) in an HTML page.

6条回答
  •  臣服心动
    2020-11-29 11:02

    jQuery has a nice KeyPress function which allows you to detect a key press, then it should be just a case of detecting the keyvalue and performing an if for the ones you want to ignore.

    edit: for example:

    $('#target').keypress(function(event) {
      if (event.keyCode == '13') {
         return false; // or event.preventDefault();
      }
    });
    

提交回复
热议问题