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: