问题
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