_javascript techniques for TextRange object processing of text part content

Source: Internet
Author: User

An object that is used to process the contents of a JavaScript object's text because the user requires a TextRange object.

TextRange is used to represent the text in HTML elements, although we usually do not use this object, but it has been provided in the IE4.0. But the invocation method provided by TextRange is rather obscure, so what can we do with it?
The traditional use of TextRange is to manipulate the text content of the mouse circle on the Web page, such as changes, deletions, additions, and so on. But its classic purpose is to find text in a Web page (this is simpler) and to get the position of the input box cursor. The latter can derive many more useful uses, such as: limit the input of the Masktextbox, its core technical point is to get the input box cursor position, and then use regular expressions to judge the input content. And then I'll introduce the "Use of arrow keys in the input box matrix of natural navigation," the core technical point is to get the input box cursor position.

The entire code that gets the cursor position in the input box is actually very short, but these objects and methods are less common.

JS Code

<span style= "Font-size:medium;" ><script language= "javascript" > 

function getcursorpsn (TXB) 

{ 

var slct = document.selection; 

var rng = Slct.createrange (); 

Txb.select (); 

Rng.setendpoint ("Starttostart", Slct.createrange ()); 

var psn = rng.text.length; 

Rng.collapse (false); 

Rng.select (); 

return PSN; 

} 

</script></span>

Here's a look at the side effects of the input box operation after using this GETCURSORPSN () method.

For an input box

HTML code

<span style= "Font-size:medium;" ><input type= "text" onkeydown= "GETCURSORPSN (This)" ></span>

It will no longer be possible to use the two arrow keys around shift+ to select text;

HTML code

<span style= "Font-size:medium;" ><textarea onkeydown= "GETCURSORPSN (This)" ></textarea></span>

, you can no longer use the shift+ up or down four arrow keys to select text. Because the code calls Rng.collapse (false) after acquiring the current cursor to the text's startpoint, it changes the editpoint of the text in the text basket.

1, to meet user requirements code snippets, using the top and bottom four keys to achieve the text box jump, and select its text box content, so as to facilitate user modification, the code is as follows:

JS Code

<span style= "Font-size:medium;" >var range = $currentTextfield. createTextRange ()//$currentTextfield for jquery object 

range.movestart (' character ', 0); 

Range.Select ();</span>

The following is an imported piece of personal feeling is also a good article about TextRange:

HTML code

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">  

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.