Override TAB inside a Browser

南楼画角 提交于 2019-12-11 03:46:06

问题


If I'm typing text in a input field and press ENTER the default behavior of all the browsers I know is to submit the form, however if I press ENTER inside a textarea a new line is added.

Is there any way to mimic this behavior (indent, not submit the form) whenever I press TAB inside a textarea? Bespin seems to do it, but in a canvas element.


回答1:


I haven't done it myself, but it seems to be possible to override the event handler and catch the key. See e.g. here.

Oh and for the JQuery crowd there even is a plugin.




回答2:


Of course there's a way. Do you use any js library? If not, the idea is just to add a keydown event handler on the textarea element, check in the handler if the keyCode of the event equals 9, and if so append a "\t" to the content of the textarea. Prototype snippet:

textarea.observe('keydown', function (e) {
  if(e.keyCode==9) {
    e.element().insert("\t");
    e.stop();
  }
}


来源:https://stackoverflow.com/questions/1973927/override-tab-inside-a-browser

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