JS get the cursor position and set the text box cursor position sample code

Source: Internet
Author: User

  This example describes how to use JavaScript to control and get a text box/text field of the mouse cursor position, the following code compatible with IE and chrome,firefox, we refer to the use of the bar

The code is as follows: <script type= "Text/javascript" >         function gettxt1cursorposition () {    & nbsp       var oTxt1 = document.getElementById ("Txt1");             var cursurposition=-1;             if (Otxt1.selectionstart) {/Non IE browser             &NBS P   cursurposition= Otxt1.selectionstart;            }else{//ie                 var range = d Ocument.selection.createRange ();                 Range.movestart ("character",-otxt1.value.length);                 cursurposition=range.text.length;                         alert (cursurposition);        }           function settxt1cursorposition (i) {  &nbsP         var oTxt1 = document.getElementById ("Txt2");             var cursurposition=-1;             if (Otxt1.selectionstart) {/Non IE browser             &NBS P   Otxt1.selectionstart=i;            }else{//ie                 var range = O Txt1.createtextrange ();                 Range.move ("character", I);                 range.select ();                     {          function Gett A1cursorposition () {            var evt =window.event?window.event: Getta1cursorposition.caller.arguments[0];             var oTa1 = document.getElementById ("ta1");             var CursurposiTion=-1;             if (Ota1.selectionstart) {/Non IE browser             &NBSP ;   cursurposition= Ota1.selectionstart;            }else{//ie                 var range = O Ta1.createtextrange ();                 Range.movetopoint (EVT.X,EVT.Y);                 Range.movestart ("character",-ota1.value.length);                 cursurposition=range.text.length;                         alert (cursurposition);        }           function setta1cursorposition (i) {      &N Bsp     var oTa2 = document.getElementById ("Ta2");             if (Ota2.selectionstart) {/Non IE browser                 ota2.selectionstart=i;                 ota2.selectionend=i;            }else{//ie                 var range = O Ta2.createtextrange ();                 Range.move ("character", I);                 range.select ();             {       }     </script>  

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.