In general programs, programmers generally like to determine the focus box by judging the legality of the content when checking the correctness of the content of the input box.
like:
Copy the code code as follows:
if(obj.value==""){
obj.focus();
return false;
}
In this way, when an input box is empty, the focus will be moved to that input box. This function is very convenient to use. But there is a small problem...
That is, after obj.focus() moves the focus to the input box, it will move the text cursor (that is, the 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 can just allow us to enter content directly in the text box without clicking the text box to make the text focus...
However, if there is already content in the text box... but the content is illegal. obj.focus() also moves the cursor to the position of the first character of the text box... At this time, the user experience will be paid attention to The designer is depressed... What we need is for the text box to get focus, and then move the text cursor to the end of the text box, so that the user can enter content directly without clicking on the text box. The input content will be appended to the original content. ..
The following code can complete this small detail:
Copy the 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 will be of great help to designers in the details of user experience...