有趣的js获取input标签中光标的索引

先看动图如下,我们就可以很清楚的知道获取input标签中光标的索引的意思了。
有趣的js获取input标签中光标的索引
由于IE支持document.selection,Firefox,Chrome,Safari以及Opera都有selectionStart和selectionEnd属性

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title></title>
	<link rel="stylesheet" href="">
</head>
<body>
	<script>
		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" >

</body>
</html>

本文转载自:https://www.cnblogs.com/xiaoyang002/p/4055716.html