JavaScript gets and sets the cursor position

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.