(i) Gets the input cursor position at the start of the selected area of the textarea or unchecked
Non-IE browsers, such as Firefox,chrome, support SelectionStart to get the starting point of the selected area, and IE does not support the attribute, which needs to be obtained indirectly through TextRange. The comparison of the starting point using the compareEndPoints method of the TextRange object can be realized.
Copy Code code as follows:
Getstartpos:function (TEXTAREA)
{
if (typeof Textarea.selectionstart!= ' undefined ')
{//Non IE
start = Textarea.selectionstart;
}
Else
{//IE
var range = Document.selection.createRange ();
var Range_textarea = Document.body.createTextRange ();
Range_textarea. Movetoelementtext (textarea);
Compare start point
for (var sel_start = 0; range_textarea. compareendpoints (' Starttostart ', range) < 0; sel_start++)
Range_textarea. MoveStart (' character ', 1);
start = Sel_start;
}//Else
return start;
}
But it's a bit of a note that under Chrome, if TEXTAREA is set to Readonley, that textarea does not appear with the input cursor, and the return SelectionStart and Selectionend are normal under 0.firefox.
(ii) Setting the area selected in textarea
Similarly, non-IE browsers support the Setselectionrange method to specify the selected range of characters, and IE does not support, but also through the TextRange to operate, where you need to pay attention to IE under the textarea of the selected interval of the relative position of the problem. As the following code is first movestart,moveend the beginning and end are set to 0,collapse move into effect, the starting point is unchanged, first moveend the end of the moving interval, and then movestart the starting point of the moving interval, before changing the point of origin, You can make sure that the relative position is constant and easier to understand.
Copy Code code as follows:
/**
* Set the selected area of the textarea
*/
Setselectrange:function (textarea, start, end)
{
if (typeof textarea.createtextrange!= ' undefined ')/IE
{
var range = Textarea.createtextrange ();
First move the relative starting point to 0.
Range.movestart ("character", 0)
Range.moveend ("character", 0);
Range.collapse (TRUE); Move the insertion cursor to the start point
Range.moveend ("character", end);
Range.movestart ("character", start);
Range.Select ();
}//If
else if (typeof textarea.setselectionrange!= ' undefined ')
{
Textarea.setselectionrange (start, end);
Textarea.focus ();
}//Else
}
After the selection of the selected area to obtain and set the method, other such as text insertion, the implementation of the replacement is relatively simple.