How to disable esc key in html page using javascript or jquery

不羁岁月 提交于 2019-12-09 15:42:01

问题


I want to disable the ESC key in an HTML page. How can I do this?

I have a form which is appearing as jQuery UI Dialog. Here if I click the ESC key, the form will close. I want to disable that.


回答1:


Browsers (Firefox, Chrome...) normally bind the Esc key to the "stop loading current page" action. Any other behaviour needs to be specifically coded with JavaScript.

The jQuery UI Dialog has a closeOnEscape setting. Just set it to false:

  • Initialize a dialog with the closeOnEscape option specified:

    $( ".selector" ).dialog({ closeOnEscape: false });
    
  • Get or set the closeOnEscape option, after init:

    //getter
    var closeOnEscape = $( ".selector" ).dialog( "option", "closeOnEscape" );
    //setter
    $( ".selector" ).dialog( "option", "closeOnEscape", false );
    



回答2:


$(document).keydown(function(e) {
    if (e.keyCode == 27) return false;
});

*thanks Johan




回答3:


The best way to do this is to figure out which dialog library you are using (colorbox? jquery ui? shadowbox) and disable the esc key (for example, in Colorbox you can set the escKey option to false).



来源:https://stackoverflow.com/questions/6729249/how-to-disable-esc-key-in-html-page-using-javascript-or-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!