码迷,mamicode.com
首页 > 其他好文 > 详细

指定光标在输入框中的位置

时间:2014-09-29 12:12:27      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   sp   div   art   

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
 
<input type="text" id="keyword" value="abcdefg" />


<script>
var Browser_Name=navigator.appName;
var isIE=(Browser_Name=="Microsoft Internet Explorer");//判读是否为ie浏览器
var textNode=document.getElementById("keyword");//keyword为要操作的文本框的ID,根据情况修改
//var count=textNode.value.length;
var count = 1;
if(isIE){//IE
    var f = textNode.createTextRange();//创建文本范围对象
    f.moveStart(character,count); //更改范围起始位置/*如果count改为0就把光标放在text中的字符的最前面*/
    f.collapse(true); //将插入点移动到当前范围的开始或结尾。
    f.select(); //将当前选中区置为当前对象,执行
}else{//FireFox
    textNode.setSelectionRange(count,count);
}
</script>  
</body>
</html>

 

指定光标在输入框中的位置

标签:style   blog   color   io   os   ar   sp   div   art   

原文地址:http://www.cnblogs.com/ahwu/p/3999502.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!