JQuery inserts a character or expression at a specified position in textarea. This example requires you to introduce the jQuery framework into the webpage and the following code should also be introduced:Copy to ClipboardReference: [www.bkjia.com] (function ($ ){
$. Fn. extend ({
InsertAtCaret: function (myValue ){
Var $ t = $ (this) [0];
If (document. selection ){
This. focus ();
Sel = document. selection. createRange ();
Sel. text = myValue;
This. focus ();
}
Else
If ($ t. selectionStart | $ t. selectionStart = '0 '){
Var startPos = $ t. selectionStart;
Var endPos = $ t. selectionEnd;
Var scrollTop = $ t. scrollTop;
$ T. value = $ t. value. substring (0, startPos) + myValue + $ t. value. substring (endPos, $ t. value. length );
This. focus ();
$ T. selectionStart = startPos + myValue. length;
$ T. selectionEnd = startPos + myValue. length;
$ T. scrollTop = scrollTop;
}
Else {
This. value + = myValue;
This. focus ();
}
}
})
}) (JQuery );
Call method:
Copy to ClipboardReference: [www.bkjia.com] $ ("# textareaId"). insertAtCaret ("new expression ");