Use JavaScript to obtain and set the cursor position in the input box (compatible with IE and Firefox)

Source: Internet
Author: User
FunctiongetLocation (elm) {if (elm. createTextRange) {// IEvarrangedocument. selection. createRange (); range. setEndPoint (& amp; #39; StartToStart & amp; #39;, elm. createTextRan

Function getLocation (elm ){
If (elm. createTextRange) {// IE
Var range = document. selection. createRange ();
Range. setEndPoint ('startstart', elm. createTextRange ());
Return range. text. length;
} Else if (typeof elm. selectionStart = 'number') {// Firefox
Return elm. selectionStart;
}
}
 
Function setLocation (elm, n ){
If (n> elm. value. length)
N = elm. value. length;
If (elm. createTextRange) {// IE
Var textRange = elm. createTextRange ();
TextRange. moveStart ('character ', n );
TextRange. collapse ();
TextRange. select ();
} Else if (elm. setSelectionRange) {// Firefox
Elm. setSelectionRange (n, n );
Elm. focus ();
}
}


Description of several functions:

IE:

TextRange. setEndPoint ('starttostart', elm. createTextRange (): Adjust the starting position of TextRange to the starting position of elm. createTextRange. The second parameter must be a TextRange. The first parameter must be of the following four values: StartToStart, StartToEnd, EndToStart, and EndToEnd, startToStart means to set the starting position of TextRange to the starting position of the second parameter. Similarly, StartToEnd means to set the starting position of TextRange to the ending position of the second parameter, so far, EndToStart and EndToEnd are well understood.
TextRange. moveStart ('character ', n): Move the starting position of TextRange to n characters. Another function is moveEnd ('character', n ), this is the termination position of the mobile TextRange. To move the termination position forward, use-n instead of n (assuming n is a positive number ).

TextRange. collapse (): Set the text insertion point to the current starting position of TextRange. To set it to the ending position, use TextRange. collapse (false ). In fact, setting the insertion point is to set the start point and end point to the same position, which is probably the reason for the name collapse. Therefore, this statement can also be replaced with textRange. moveEnd ('character ', n-elm. value. length), but it does not seem so concise.

For TextRange objects, query the DOM manual of IE.

 

Firefox:

Firefox is much simpler, Element. setSelectionRange (start, end): select the text from the start position to the end position in the Element text. Set the insertion point as above, set the start and end positions to the same one.


From orain's column

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.