Some operations of the cursor summary _javascript tips

Source: Internet
Author: User

The range operation under IE is much stronger than that of Mozilla, where only the operation under IE is discussed.
Here are some features of cursor positioning:

1. The cursor does not change

Direct Obj.focus (), the cursor will return to the previous position, that is, the position is unchanged

2. The cursor is in the front

Copy Code code as follows:

var r = Obj.createtextrange ();
R.collapse ();
R.select ();

Use this method to make the cursor precede the input box

3. At the end of the cursor

Copy Code code as follows:

var r = Obj.createtextrange ();
R.collapse (FALSE);
R.select ();

Use this method to make the cursor stop at the end of the input box

4. Select the MoveStart or MoveEnd method for the range you want to use for some of the input box, and use the detailed method to refer to MSDN.

<script type= "Text/javascript" > Function sl (o, m, n) {var rt = O.createtextrange (); Rt.collapse (); Rt.select ();//cursor set the top var r = Document.selection.createRange (); R.collapse (FALSE); R.movestart ("character", m);//starting from M bit R.moveend ("character", n);//select N bit r.select (); } </script> <input name= "a" value= "123456789" ><input type=button value= "select" onclick= "SL (a,2,4)" >
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


5. To extend the movement and position of the cursor, this is a frequently asked question
<input name=a value= "123456789" > <input type=button value= "forward" onclick= "SetPos (a,-1)" > <input type= Button value= "Back" onclick= "SetPos (a,1)" > <input Type=button value= "position of current cursor" onclick= "GetPos (a)" > <script Type= "Text/javascript" > Function SetPos (obj,n) {obj.focus (); var r = Document.selection.createRange (); R.collapse (FALSE); R.move ("character", N); R.select (); }; function GetPos (obj) {obj.focus ();//cursor position unchanged var r = Document.selection.createRange (); R.collapse (FALSE); R.setendpoint ("Starttostart", Obj.createtextrange ()); alert (r.text.length); }; </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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.