One. Get cursor Position:
Gets the cursor position function getcursortposition (textdom) { var cursorpos = 0; if (document.selection) { //IE support textdom.focus (); var selectrange = Document.selection.createRange (); Selectrange.movestart (' character ',-textdom.value.length); Cursorpos = selectRange.text.length; } else if (Textdom.selectionstart | | textdom.selectionstart = = ' 0 ') { //Firefox support Cursorpos = Textdom.select Ionstart; } return cursorpos;}
Two. Set the cursor position:
Set cursor position function setcaretposition (Textdom, POS) { if (textdom.setselectionrange) { //IE support Textdom.focus (); Textdom.setselectionrange (POS, POS); } else if (textdom.createtextrange) { ///Firefox support var range = Textdom.createtextrange (); Range.collapse (true); Range.moveend (' character ', POS); Range.movestart (' character ', POS); Range.Select (); }}
Three. Get the selected text:
Gets the selected text function Getselecttext () { var userselection, text; if (window.getselection) { ///Firefox support userselection = Window.getselection (); } else if ( document.selection) { //IE support userselection = Document.selection.createRange (); } if (! ( Text = Userselection.text)) { text = userselection; } return text;}
Four. Select a specific range of text:
/*** Select a specific range of text * Parameters: * textdom [JavaScript DOM String] Current Object * startpos [int] Start position * endpos [int] End position */functi On Setselecttext (Textdom, startpos, endpos) {var startpos = parseint (startpos), Endpos = parseint (Endpos), TextLength = TextDom.value.length; if (textLength) {if (!startpos) {startpos = 0; } if (!endpos) {endpos = TextLength; } if (Startpos > TextLength) {startpos = TextLength; } if (Endpos > TextLength) {endpos = TextLength; } if (Startpos < 0) {startpos = textLength + startpos; } if (Endpos < 0) {Endpos = textLength + endpos; } if (Textdom.createtextrange) {//IE support var range = Textdom.createtextrange (); Range.movestart ("character",-textlength); Range.moveend ("character",-textlength); Range.movestart ("character", startpos); Range.moveend ("character", Endpos); Range.Select (); }else{//Firefox support Textdom.setselectionrange (startpos, endpos); Textdom.focus (); } }}
Five. Insert text after the cursor:
/*** insert text after cursor * parameter: * textdom [JavaScript DOM String] Current Object * value [Stri NG] The text to be inserted */function Insertaftertext (textdom, value) {var selectrange; if (document.selection) {//IE support Textdom.focus (); Selectrange = Document.selection.createRange (); Selectrange.text = value; Textdom.focus (); }else if (Textdom.selectionstart | | textdom.selectionstart = = ' 0 ') {//Firefox support var startpos = TEXTD Om.selectionstart; var endpos = textdom.selectionend; var scrolltop = textdom.scrolltop; Textdom.value = textDom.value.substring (0, startpos) + value + textDom.value.substring (Endpos, textDom.value.length); Textdom.focus (); Textdom.selectionstart = Startpos + value.length; Textdom.selectionend = Startpos + value.length; Textdom.scrolltop = scrolltop; } else {textdom.value + = value; Textdom.focus (); }}
JavaScript gets and sets the cursor position