Copy Code code as follows:
Inserts 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 ();
}
}
Here is the cloud-dwelling community Demo code:
<script>//Inserts a string at the cursor//MyField text box object//value function to be inserted 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 = Myfiel D.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 (); } </script> <form> <textarea id=demo > position the mouse to any location here, and then click the button below to test the effect </textarea> <input Type=button onclick=insertatcursor (document.getElementById (' demo '), "cloud-dwelling community") value= cloud-dwelling community id=button1> <input type =button onclick=insertatcursor (document.getElementById (' demo '), "jb51.net") value=jb51.net id=button2> >
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]