Javascripttextarea cursor locating method (compatible with IE and FF) _ javascript skills

Source: Internet
Author: User
The main method is to implement the optical mark Locating Method in textarea. Considering the compatibility of multiple browsers, you can refer to the following for more information. Today, I am working on a blog forwarding function. Just like the function of Sina's meager broadcasting, there is a textArea, which is the box where we send a meager message, when someone else clicks this forwarding button, his meager content will enter textArea. The difficulty is that, at this time, the cursor is positioned at the beginning.

So I searched baidu, google, and found
Methods in IE

The Code is as follows:


Var tea = document. getElementById ("text box ID ");
Var txt = textArea. createTextRange ();
Txt. moveEnd ("character", 0-tempText.text.length );
Txt. select ();


However, this method is only available in the IE browser, so I found a blog by searching on the Internet.
Google has been using various methods for a long time. Most of them do not support Internet Explorer or Internet Explorer ONLY. In the end, master told me that an old page contains this function and found the code and tried it, IE and FF are both successful!
Share the code

The Code is as follows:


Function locatePoint (){
Var aCtrl = document. getElementById ("txtContent ");
If (aCtrl. setSelectionRange ){
SetTimeout (function (){
ACtrl. setSelectionRange (0, 0); // positions the cursor at the beginning of textarea. If you want to locate another position, modify it.
ACtrl. focus ();
}, 0 );
} Else if (aCtrl. createTextRange ){
Var textArea = document. getElementById ("txtContent ");
Var tempText = textArea. createTextRange ();
TempText. moveEnd ("character", 0-tempText.text.length );
TempText. select ();
}
}


Find the method below FF

The Code is as follows:


Var tea = document. getElementById ("text box ID ");
Tea. setSelectionRange (0, 0); // positions the cursor at the beginning of textarea. If you want to locate another position, modify it.
Tea. focus ();


Therefore, we can use an if method to add and judge the compatibility. The integration method is as follows, just as in the post.
Html section

The Code is as follows:



Forwarding


JS Section

The Code is as follows:


Related Article

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.