_javascript techniques for the selected area processing of JS textarea

Source: Internet
Author: User
(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.

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.