js案例01-设置光标到指定input
代码如下
<body>
<input type="text" name="username" value="litong" />
<input type="text" name="password" value="00000000" />
<input type="button" onclick="change('username')">
<input type="button" onclick="change('password')">
</body>
<script>
function change(name) {
var input = document.getElementsByName(name)[0];
if(input.setSelectionRange) {
input.focus();
input.setSelectionRange(0, input.value.length);
} else if(ctrl.createTextRange) { //兼容IE 浏览器
var range = ctrl.createTextRange();
range.collapse(true);
range.moveStart('character', 0);
range.moveEnd('character', input.value.length);
range.select();
}
}
</script>
input.focus();
文本域获取焦点
in