首页 > 代码库 > javascript笔记——js获取input标签中光标的索引

javascript笔记——js获取input标签中光标的索引

出处:http://www.cnblogs.com/MrZouJian/p/5850553.html

function getTxt1CursorPosition(){
var oTxt1 = document.getElementById("txt1");
var cursurPosition=-1;
if(oTxt1.selectionStart){//非IE浏览器
cursurPosition= oTxt1.selectionStart;
}else{//IE
var range = document.selection.createRange();
range.moveStart("character",-oTxt1.value.length);
cursurPosition=range.text.length;
}
alert(cursurPosition);
}
</script>
<input type="text" id="txt1" value="http://www.mamicode.com/abcde" onclick="getTxt1CursorPosition()">

 

javascript笔记——js获取input标签中光标的索引