Example
| The code is as follows: |
Copy code |
<Script type = "text/javascript"> JQuery (function () { JQuery ('. content textarea'). keyup (function (e ){ If (e. keyCode = 13 ){ Var curr = getCaret (this ); Var val = jQuery (this). val (); Var end = val. length; JQuery (this). val (val. substr (0, curr) + '<br/>' + val. substr (curr, end )); } }) }); Function getCaret (el ){ If (el. selectionStart ){ Return el. selectionStart; } Else if (document. selection ){ El. focus (); Var r = document. selection. createRange (); If (r = null ){ Return 0; } Var re = el. createTextRange (), Rc = re. duplicate (); Re. moveToBookmark (r. getBookmark ()); Rc. setEndPoint ('endstart', re ); Return rc. text. length; } Return 0; } </Script> Html <Div class = "content"> <Textarea> press enter to add the br tag at the cursor position </textarea> </Div> |
Supplement: enter the key in the current text box to the tab key
| The code is as follows: |
Copy code |
<Script type = "text/javascript"> // Function: convert the Enter key to the tab key. JQuery (function (){ JQuery ('input: text: first '). focus (); Var $ indium = jQuery ('input: text '); $ Indium. bind ('keylow', function (e ){ Var key = e. which; If (key = 13 ){ E. preventDefault (); Var nxtIdx = $ indium. index (this) + 1; JQuery (": input: text: eq (" + nxtIdx + ")"). focus (); } }); }); </Script> |