<div contenteditable="true"></div>?
我在这里面输入了123456789这个几个字符?
当我把鼠标放在某个数字的后面时,要获取鼠标是在哪个位置?
比如我的光标在3后面,应该得到3,在1前面应该是0,在6后面应该是6?
就是当我输入任意字符时,获得光标所在的是什么位置??
我要的是ie的方法
?
?
2010-05-26?johnson.lee?(高级程序员)
- <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.0?Transitional//EN">??
- <html>??
- ????<head>??
- ????????<title>?new?document?</title>??
- ????</head>??
- ????<body>??
- ????????<div?contentEditable="true"?style="height:50px;?border:2px?solid?red;"></div>??
- ????????光标前的字符:<span?id="selection"></span>??
- ????????<script?language="javascript">??
- ????????<!--??
- ????????????var?cursor?=?0;//?光标位置??
- ??
- ????????????document.onselectionchange?=?function()?{??
- ????????????????var?range?=?document.selection.createRange();??
- ????????????????var?srcele?=?range.parentElement();??
- ????????????????var?copy?=?document.body.createTextRange();??
- ????????????????copy.moveToElementText(srcele);??
- ??
- ????????????????for?(cursor?=?0;?copy.compareEndPoints("StartToStart",?range)?<?0;?cursor++)?{??
- ????????????????????copy.moveStart("character",?1);??
- ????????????????}??
- ????????????????document.getElementById("selection").innerText?=?srcele.innerText.substring(cursor?-?1,?cursor);??
- ????????????}??
- ????????//-->??
- ????????</script>??
- ????</body>??
- </html>??