Code _javascript tips for using TextRange to get the position of the cursor in the input box

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

To thoroughly understand the specific use of TextRange, you need to know something about it, please refer to MSDN.



Here's a look at the side effects of the input box operation after using this GETCURSORPSN () method. For the input box <input type= "text" onkeydown= "GETCURSORPSN (This)", it will not be able to use the shift+ around these two arrow keys to select text; for <textarea onkeydown = "GETCURSORPSN (This)" ></textarea&gt, you can no longer use 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. But this side effect basically does not give us to use the text box to bring any big problem, therefore basically does not care too much.

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.