Copy codeThe Code is as follows:
// Insert a string at the cursor
// MyField text box object
// The value to insert
Function insertAtCursor (myField, myValue)
{
// IE support
If (document. selection)
{
MyField. focus ();
Sel = document. selection. createRange ();
Sel. text = myValue;
Sel. select ();
}
// MOZILLA/NETSCAPE support
Else if (myField. selectionStart | myField. selectionStart = '0 ')
{
Var startPos = myField. selectionStart;
Var endPos = myField. selectionEnd;
// Save scrollTop before insert
Var restoreTop = myField. scrollTop;
MyField. value = myField. value. substring (0, startPos) + myValue + myField. value. substring (endPos, myField. value. length );
If (restoreTop> 0)
{
// Restore previous scrollTop
MyField. scrollTop = restoreTop;
}
MyField. focus ();
MyField. selectionStart = startPos + myValue. length;
MyField. selectionEnd = startPos + myValue. length;
} Else {
MyField. value + = myValue;
MyField. focus ();
}
}
The Demo code is as follows:
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]