Today in doing a blog forwarding function, like the Sina Weibo broadcast of that function, there is a textarea, that is, we send a meager box, others have a meager button, when we click on the forwarding button, his meager will enter into the textarea, What looks like a very simple text box assignment is, in fact, the difficulty is that the cursor is positioned at the front.
So he searched the Baidu,google and found the
The method below IE
Copy Code code as follows:
var Tea=document.getelementbyid ("ID of text box");
var txt=textarea.createtextrange ();
Txt.moveend ("character", 0-temptext.text.length);
Txt.select ();
But this method is only available under IE's browser, so a blog was found through web search
Google for a long time, tried a variety of methods, most of them are not support IE is IE only, finally Master told me an old page has this function, found that code tried, ie and FF have been successful!
Share the Code
Copy Code code as follows:
function Locatepoint () {
var Actrl = document.getElementById ("txtcontent");
if (Actrl.setselectionrange) {
settimeout (function () {
Actrl.setselectionrange (0, 0); Position the cursor at the beginning of the textarea, you need to navigate to a different location.
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 ();
}
}
Found the method below ff.
Copy Code code as follows:
var Tea=document.getelementbyid ("ID of text box");
Tea.setselectionrange (0, 0); Position the cursor at the beginning of the textarea, you need to navigate to a different location.
Tea.focus ();
So the compatible method we can use an if join to judge, the integration method is as follows, also like that in the post
HTML section
Copy Code code as follows:
<input id= "Tea" type= "text" size= "value=" ">
<button onclick= "xx ()" > Forwarding </button>
JS part
Copy Code code as follows:
<script language= "JavaScript" >
var tea = document.getElementById ("Tea");
Function Locatepoint () {
if (tea.setselectionrange) {
settimeout (function () {
Tea.setselectionrange (0 , 0); Position the cursor at the beginning of the textarea, and you need to navigate to a different location to modify the
Tea.focus ().
}, 0);
}else if (tea.createtextrange) {
var txt=tea.createtextrange ();
Txt.moveend ("character", 0-txt.text.length);
Txt.select ();
}
}
function xx () {
Tea.value = ' BBB ';
Locatepoint ();
}
</script>