In a general program. When the programmer makes the content correctness of the input box, it usually likes to determine the focus box by judging the legality of the content.
Such as:
Copy Code code as follows:
if (obj.value== "") {
Obj.focus ();
return false;
}
This moves the focus to the input box when the input box is empty. This feature is very convenient to use. But there is a small problem ...
That's Obj.focus () when you move the focus to the input box, you move the text cursor (that is, a flashing vertical line) to the position of the first character of the input box ... As far as the above judgment is concerned. If there is no content in the text box ... Obj.focus just satisfies the point where we can just type in the text box without clicking the text box to keep the text focused ...
However, if there is already content in the text box ... But this content is illegal. Obj.focus () also moves the cursor to the position of the first character of the text box ... This will let the attention of the user experience of the designer depressed ... What we need is that the text box gets the focus, then the text cursor moves to the end of the text box, allowing the user to enter the content without clicking the text box. The input will be appended to the original content.
The following code can complete this small detail:
Copy Code code as follows:
<script language= "JavaScript" >
function Getselectpos (obj) {
var esrc = document.getElementById (obj);
if (esrc==null) {
Esrc=event.srcelement;
}
var rtextrange =esrc.createtextrange ();
Rtextrange.movestart (' character ', esrc.value.length);
Rtextrange.collapse (TRUE);
Rtextrange.select ();
}
</script>
This code is a great help to the designer in the details of the user experience ...