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